Gui freeezing when using threading Gui freeezing when using threading multithreading multithreading

Gui freeezing when using threading


You need to reverse your method. The GUI needs to stay in the main thread while the work is done in a "worker thread" (typically a BackGroundWorker). Then the worker reports back to the GUI which then updates.


You'd better do the opposite. Make your intensive work in the thread (or a background worker), and show the wait screen in the main application thread.


You need to use a BackgroundWorker. Drag one onto your form, click backgroundWorker1 and set the WorkerReportsProgress property to True

Then goto the events (via the properties window) and attach handlers for

  • DoWork, this is where all the work that is represented by the progress bar. You will "report progress" via this and the background worker will make sure ProgressChanged is called on the UI thread.
  • ProgressChanged, this is where you update the UI based on progress and state data reported to the method

DoWork event looks something like this

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e){    var userState = new StateClass();    while (working)    {        // TODO: do work here        // update the state surrounding this task via userState        userState.property = "some status";        // report the progress so that backgroundWorker1_ProgressChanged gets called         this.backgroundWorker1.ReportProgress(percentComplete, userState);    }}

ProgressChanged event looks like this

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e){    // e.UserState contains the state data you passed to ReportProgress,     //   you have to cast it to the right type though, since its of type object    var userState = (StateClass)e.UserState;    int progress = e.ProgressPercentage;    // TODO: report progress to the UI with the above variables}

Now all you have to do is tell the background worker to do work by calling this.backgroundWorker1.RunWorkerAsync()