2

I have a WPF application which is built on the MVVM design pattern.

I wish to implement a progress bar in the app, that follows the MVVM pattern.

I found some solutions to implement this using background-worker class. For some reasons i am not allowable to use background worker.

Does any one have any suggestions on how to implement this?

Thanks in advance

7
  • Implementing Progress bar in an MVVM application is not a big deal, but what you have tried so far, and what issue are u facing to implement? Commented May 15, 2015 at 7:06
  • Depends very much on Progress of What. Commented May 15, 2015 at 7:16
  • Does it means that you're not allowed to use background threads at all or just the background worker? If that's the case your UI will lock up until the whole operation has finished. Commented May 15, 2015 at 10:10
  • @VimalCK the problem is not updating progressbar. How can i update in a thread manner. Commented May 15, 2015 at 10:18
  • @XAMlMAX i am working on an ArcGiS solution which doesn't support bacground-worker. that's my problem. Commented May 15, 2015 at 10:21

2 Answers 2

2

I would prefer Task over background workers. Anyway, here's a simple implementation of progress bar.

<ProgressBar Value="{Binding ProgressValue}" Maximum="100" Height="20" />

 

public MainViewModel()
{
    Task.Run(() =>
        {
            while (true)
            {
                ProgressValue++;
                Thread.Sleep(250);
            }
        });
    }

    public int ProgressValue
    {
        get { return _progressValue; }
        set { _progressValue = value; OnPropertyChanged(); }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The basics are very simple.

View:

 <ProgressBar Value="{Binding ProgressValue}" />

ViewModel:

 public double ProgressValue { get/set with INPC }

But the remaining challenge is to set the ViewModels ProgressValue from a non-blocking process in a thread-safe manner.

Your question lacks the details to help with that.

3 Comments

the problem with this solution is that it make my UI unresponsive.
Yes, I said 'non blocking'. But for the how of that you need to be more specific.
Bindings in 4.0 and above automatically marshal INPC events onto the UI thread, so you can set the property values from a background thread without worry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.