How can I create WPF controls in a background thread?

后端 未结 8 772
终归单人心
终归单人心 2021-01-17 15:49

I have method which create background thread to make some action. In this background thread I create object. But this object while creating in runtime give me an exception :

8条回答
  •  轮回少年
    2021-01-17 16:16

    Try following Code:

    public void SomeMethod() 
    { 
    
    System.ComponentModel.BackgroundWorker myWorker = new  System.ComponentModel.BackgroundWorker();
    
    myWorker.DoWork += myWorker_DoWork;
    
    myWorker.RunWorkerAsync();
    
    }
    
    private void myWorker_DoWork(object sender,
       System.ComponentModel.DoWorkEventArgs e)
    {
       // Do time-consuming work here
    }
    

提交回复
热议问题