Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVVM multithreading
I am writing a WPF(MVVM) application that would run a process in a background thread.
Here is the particualr method that is running on a different thread than the UI thread.
 private void ExecuteAsync()
{
 IsRunning = true;
 for (int i = 0; i< 1000; i++)
 {
     App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
 () =>; TheTextCollection.Add(string.Concat(string.Format("{0} ", i), _theTextFilled))));
     App.Current.Dispatcher.Invoke(DispatcherPriority.Normal,new Action(
                    ()=>;ProgressPercentage=(int)(100*((float)i/(float)1000))));
            }
     App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
 () =>; ProgressPercentage = 0));
IsRunning = false;       
}
In the above code:
 
1) ProgressPercentage is a ViewModel property of type int which is two way data-bound to a progress bar in the UI.
 
2)TheTextCollection is a property of type ObservableCollection<string> which is one way databound to the items property of a listbox in the UI.
 
3)There is a button which starts the process and it gets disabled once the process starts (as a typical multithreaded UI would behave.)IsRunning is a variable which is used by the relay command that is bound to the Command property of the button.
 
The button very finely gets disabled when the process starts but it does not gets enabled on its own after the process ends. However if the some form event is raised it gets enabled (e.g.: scrolling of the listbox or maximizing the form). Any suggestion would be appreciated.
Posted 18-Apr-11 4:38am
Edited 19-Apr-11 2:58am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can avoid float calculations if possible - as far as i know, progress bar understand int so why use float - float calculations are slower. Other than that, this looks good code IMO.
  Permalink  
Comments
babai28 at 19-Apr-11 5:09am
   
You may percieve that the code is converting the value to "int" before equating the value to the ProgressPercentage property. Can you think of another way to supply the percentage dynamically in this particular situation?
The question had not got anything to do with float/int thing. It is a mock situation to demonstrate the problem.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If the button's enabled/disabled via commands, then you can try a call to CommandManager.InvalidateRequerySuggested.
 
More info: http://msdn.microsoft.com/en-us/library/system.windows.input.commandmanager.invalidaterequerysuggested.aspx[^]
 
Call it once the thread ends.
  Permalink  
v3
Comments
babai28 at 19-Apr-11 4:10am
   
Hi,
Thanks for the reply. I understand CommandManager is a static class. Can you please provide a code example over the existing code. That would provide a better understanding.
Basically I understand that an event is required to refresh the commnd binding of the button. But I don't know how to do it withing the limitation of MVVM.
Nishant Sivakumar at 19-Apr-11 8:38am
   
Oops, my bad. I have updated my answer with the correct method :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

The solution is call of the static method
CommandManager.InvalidateRequerySuggested()
The thing is Josh Smith's relay command is good whenever the user interacts witht the form. However if the valus is changed by some other thread/timer/program the Enable/Disable thing does not work automatically.
Here is the MSDN link
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 389
1 Nirav Prabtani 268
2 Richard Deeming 250
3 _Amy 185
4 CHill60 170
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,067
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 19 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100