Click here to Skip to main content
16,020,822 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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
Updated 19-Apr-11 2:58am
v3

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.
 
Share this answer
 
v3
Comments
Siddhartha S. 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.
Nish Nishant 19-Apr-11 8:38am    
Oops, my bad. I have updated my answer with the correct method :-)
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
 
Share this answer
 
v2
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.
 
Share this answer
 
Comments
Siddhartha S. 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900