How can I create WPF controls in a background thread?

后端 未结 8 598
终归单人心
终归单人心 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:33
    void Background_Method(object sender, DoWorkEventArgs e) 
    { 
        TreeView tv = new TreeView(); 
        // Generate your TreeView here
        UIDispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => 
        { 
            someContainer.Children.Add(tv);
        }; 
    }
    
    0 讨论(0)
  • 2021-01-17 16:34

    I solved my problem. I just used e.Result property of RunWorkerCompleted method. I get data in background thread and then use this data when thread completed. Thank every body for useful methods. Special thank to Veer to give a recommendation about e.Result property.

    0 讨论(0)
提交回复
热议问题