Gui freezezing when using thread

I was stuck and hoped that someone could help me.

I created a / gui class with a loading panel set to highlight so that when I complete a task, I can display it to the user.

In one of my gui classes, in the constructor on the first line, I make a new instance of this class and then do

    LoadingBar bar = new LoadingBar();
    Thread thread = new Thread(bar.Show);
    thread.Start();

However, even in the main program flow more intense material happens, this gui still seems to freeze, even if I use the backround worker.

Is there something wrong with the approach I mentioned, and if so, what do I need to change?

thank

+5
source share
4 answers

. , " " ( , BackGroundWorker). , .

+6

. ( ) .

+2

BackgroundWorker. , backgroundWorker1 WorkerReportsProgress True

( )

  • DoWork, , . " ", , ProgressChanged .
  • ProgressChanged, ,

DoWork :

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

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
}

, , , this.backgroundWorker1.RunWorkerAsync()

+1

, , . , , , .

System.Threading.Thread.Sleep(1);

0

All Articles