How can I make a background worker thread set to Single Thread Apartment?

前端 未结 5 947
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 07:34

I am creating an automated test running application. In this part of the application, I am working on a polling server. It works by constantly polling the web server to de

5条回答
  •  佛祖请我去吃肉
    2020-11-27 08:01

    I have not tested it, but if you invoke the WinForms Form, you should be back to the UI thread and most of the stuff should work again.

    BackgroundWorker bgw = new BackgroundWorker();
    bgw.DoWork += new DoWorkEventHandler(this.bgw_DoWork);
    bgw.RunWorkerAsync();
    
    private void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        // Invoke the UI thread
        // "this" is referring to the Form1, or what ever your form is
        this.Invoke((MethodInvoker)delegate
        {
            Clipboard.GetText();
            // etc etc
        });
    }
    

提交回复
热议问题