How to use WPF Background Worker

前端 未结 4 1163
孤独总比滥情好
孤独总比滥情好 2020-11-22 05:53

In my application I need to perform a series of initialization steps, these take 7-8 seconds to complete during which my UI becomes unresponsive. To resolve this I perform t

4条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 06:45

    I found this (WPF Multithreading: Using the BackgroundWorker and Reporting the Progress to the UI. link) to contain the rest of the details which are missing from @Andrew's answer.

    The one thing I found very useful was that the worker thread couldn't access the MainWindow's controls (in it's own method), however when using a delegate inside the main windows event handler it was possible.

    worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
    {
        pd.Close();
        // Get a result from the asynchronous worker
        T t = (t)args.Result
        this.ExampleControl.Text = t.BlaBla;
    };
    

提交回复
热议问题