A very basic explanation for threading in WPF?

前端 未结 3 1433
刺人心
刺人心 2021-01-06 11:49

I am very very new to WPF. I looked in the internet for several examples and tutorials about threading. They\'ve their own way to describe. But for naive like me, I want to

3条回答
  •  执念已碎
    2021-01-06 12:24

    You need to use the BackgroundWorker for your long running task and use Dispatcher to update UI in between

     //create background worker
     BackgroundWorker worker = new BackgroundWorker();
     //assign it work
     worker.DoWork += new DoWorkEventHandler(worker_DoWork);
     //start work
     worker.RunWorkerAsync();
    
    
    //this work will be done in background
    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        SET initial = 0;
        SET maxData = 1000
        DO UNTIL initial <1000
       CREATE db query "INSERT INTO (col1,col2,col3) VALUES(value1,value2,value3);"
    
       //in between your work do this to update label
       label.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,new Action(delegate()
            {
             Label.Content = "SomeValue";
            }
            ));
       END DO
      }
    

提交回复
热议问题