zoukankan      html  css  js  c++  java
  • WPF BackgroundWorker The calling thread must be STA, because many UI components require this.【原创】

    1)Issue:

          private void button1_Click(object sender, RoutedEventArgs e)
            {
                BackgroundWorker bw_test = new BackgroundWorker();
                bw_test.DoWork += new DoWorkEventHandler(bw_test_DoWork);
                bw_test.RunWorkerAsync();
            }
            
            void bw_test_DoWork(object sender, DoWorkEventArgs e)
            {
                try
                {
                    //show another Forms
                    InputCvtDefManul put = new InputCvtDefManul();
                    put.ShowDialog();
                }
                catch (Exception ex)
                { }
            }
    

     Encounter this error:  The calling thread must be STA, because many UI components require this

    2) 经过尝试,有两种方式可以解决:

    A.这种方法网上有很多,如果不需要交互(等待显示Form的数据),或者仅仅是更新其显示Form的数据,这个就足够解决了。 

           void bw_test_DoWork(object sender, DoWorkEventArgs e)
            {
                try
                {
    
                    //InputCvtDefManul put = new InputCvtDefManul();
                    //put.ShowDialog();
                    Thread newWindowThread = new Thread(new ThreadStart(() =>
                            {
                                Dispatcher.Invoke(new Action(() =>
                                    {
                                        InputCvtDefManul ss = new InputCvtDefManul();
                                        ss.ShowDialog();
                                    }));
                            }));
                    newWindowThread.Start();
                    newWindowThread.SetApartmentState(ApartmentState.STA);
                    newWindowThread.IsBackground = false;
                    newWindowThread.Start();
                }
                catch (Exception ex)
                { }
            }
    

    B.使用当前进程:

          void bw_test_DoWork(object sender, DoWorkEventArgs e)
            {
                try
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new DoMywork(ThreadStartingPoint));
    
                    MessageBox.Show("iii", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
    
                }
                catch (Exception ex)
                { }
            }
            private delegate void DoMywork();
            private void ThreadStartingPoint()
            {
    
                InputCvtDefManul put = new InputCvtDefManul();
                put.ShowDialog();
            }
    

     这样的结果是只有在另起的Form关闭后才会弹出一个MessageBox。(异步)

  • 相关阅读:
    将本地html文件拖到IE8浏览器无法打开,直接弹出一个下载的对话框
    ImageMagick
    64位win8.1系统 运行 32位程序,文件夹路径是中文遇到问题
    Dreamweaver
    JBOSS Spring Web
    spring web应用
    SQL PKG示例
    SQL分区表示例
    Java RMI 框架(远程方法调用)
    Java NIO 进程间通信
  • 原文地址:https://www.cnblogs.com/alexzp/p/3664124.html
Copyright © 2011-2022 走看看