I am having fun with WPF and got a problem. I have googled and found this website that has the same problem of me but without any working solution.
The problem is that I have a button that do some processing of data (around 30 sec). I want to have the button to disable and to have log writing in a text box… the problem is that it doesn’t disable and it doesn’t wrote any thing on the textbox until the processing is completely done.
Any idea?
private void button1_Click(object sender, RoutedEventArgs e) { this.button1.IsEnabled = false; //Long stuff here txtLog.AppendText(Environment.NewLine + 'Blabla'); //End long stuff here this.button1.IsEnabled = true; }
As others have said, use the
BackgroundWorkeror some other method of doing work asychronously.You can declare it under your
Window, initialize it somewhere like theLoadedevent, and use it in theClickevent. Here’s your method, modified to useBackgroundWorker, assuming you’ve declared it under theWindowas_bw:Note that anything that modifies your UI from another thread must be done within a
Dispatcher.InvokeorDispatcher.BeginInvokecall, WPF does not allow you to get or setDependencyPropertyvalues from any thread but the one where the object was created (more about this here).If you wanted to read from
txtLoginstead of modifying it, the code would be the same: