<bdo id='Lhm8J'></bdo><ul id='Lhm8J'></ul>
  • <small id='Lhm8J'></small><noframes id='Lhm8J'>

    1. <legend id='Lhm8J'><style id='Lhm8J'><dir id='Lhm8J'><q id='Lhm8J'></q></dir></style></legend>
      1. <tfoot id='Lhm8J'></tfoot>
      2. <i id='Lhm8J'><tr id='Lhm8J'><dt id='Lhm8J'><q id='Lhm8J'><span id='Lhm8J'><b id='Lhm8J'><form id='Lhm8J'><ins id='Lhm8J'></ins><ul id='Lhm8J'></ul><sub id='Lhm8J'></sub></form><legend id='Lhm8J'></legend><bdo id='Lhm8J'><pre id='Lhm8J'><center id='Lhm8J'></center></pre></bdo></b><th id='Lhm8J'></th></span></q></dt></tr></i><div id='Lhm8J'><tfoot id='Lhm8J'></tfoot><dl id='Lhm8J'><fieldset id='Lhm8J'></fieldset></dl></div>

      3. 在 BackgroundWorker 运行时显示模态窗口,而不会出现 STA/MTA 问题

        Showing Modal Window while BackgroundWorker runs, without getting STA/MTA issue(在 BackgroundWorker 运行时显示模态窗口,而不会出现 STA/MTA 问题)
            <tfoot id='AmkYa'></tfoot>

            <legend id='AmkYa'><style id='AmkYa'><dir id='AmkYa'><q id='AmkYa'></q></dir></style></legend>
              <tbody id='AmkYa'></tbody>
              <bdo id='AmkYa'></bdo><ul id='AmkYa'></ul>
            • <i id='AmkYa'><tr id='AmkYa'><dt id='AmkYa'><q id='AmkYa'><span id='AmkYa'><b id='AmkYa'><form id='AmkYa'><ins id='AmkYa'></ins><ul id='AmkYa'></ul><sub id='AmkYa'></sub></form><legend id='AmkYa'></legend><bdo id='AmkYa'><pre id='AmkYa'><center id='AmkYa'></center></pre></bdo></b><th id='AmkYa'></th></span></q></dt></tr></i><div id='AmkYa'><tfoot id='AmkYa'></tfoot><dl id='AmkYa'><fieldset id='AmkYa'></fieldset></dl></div>

                <small id='AmkYa'></small><noframes id='AmkYa'>

                  本文介绍了在 BackgroundWorker 运行时显示模态窗口,而不会出现 STA/MTA 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个 WPF 应用程序.我有一个耗时的方法,我想通过 BackgroundWorker 运行异步.当方法运行时,我想显示一个模态的请稍候..."对话框窗口,它必须在 BackgroundWorker 完成时自动关闭.

                  I am working on a WPF application. I have a time consuming method that I want to run async via BackgroundWorker. While the method runs, I want to display a modal "Please Wait..." dialog window, which must automatically close when the BackgroundWorker completes.

                  我目前对 BackgroundWorker 或任何多线程编程的经验很少.

                  I currently have very little experience with BackgroundWorker or any multi threaded programming.

                  下面的代码当前导致 InvalidOperationException,并带有消息调用线程必须是 STA,因为许多 UI 组件都需要这个."

                  The code below currently results in an InvalidOperationException, with the message "The calling thread must be STA, because many UI components require this."

                  请告诉我如何实现我想要实现的目标,如果你能帮助我了解问题所在,请给我额外的加分.

                  Please advise me on how to achieve what I am trying to achieve, and extra brownie-points if you can help me understand what is going wrong.

                  非常感谢!

                  编辑澄清一下 - 这个想法是主线程启动 BackgroundWorker,然后显示模式对话框.当工作人员完成时,它会关闭模式对话框.当模态对话框关闭时,主线程继续.

                  EDIT Just to clarify - The idea is that the main thread launches the BackgroundWorker, then shows the modal dialog. When the worker completes, it closes the modal dialog. When the modal dialog closes, the main thread continues.

                  public class ImageResizer
                  {
                      private BackgroundWorker worker;
                      private MemoryStream ImageData { get; set; } // incoming data
                      private public MemoryStream ResizedImageData { get; private set; } // resulting data
                      private Dialogs.WorkInProgressDialog ProgressDialog;
                  
                  
                      // Public interface, called by using class:
                      public MemoryStream ReduceImageSize(MemoryStream imageData)
                      {
                          // injected data:
                          this.ImageData = imageData;
                  
                          // init progress dialog window:
                          ProgressDialog = new Dialogs.WorkInProgressDialog();
                  
                          // Start background worker that asyncronously does work
                          worker = new BackgroundWorker();
                          worker.DoWork += new DoWorkEventHandler(worker_DoWork);
                          worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);            
                          worker.RunWorkerAsync();
                  
                          // Show progress dialog. Dialog is MODAL, and must only be closed when resizing is complete
                          ProgressDialog.ShowDialog(); // THIS LINE CAUSES THE INVALID OPERATION EXCEPTION
                  
                          // This thread will only continue when ProgressDialog is closed.
                  
                          // Return result
                          return ResizedImageData;
                      }
                  
                      private void worker_DoWork(object sender, DoWorkEventArgs e)
                      {
                          // Call time consuming method
                          ResizedImageData = ReduceImageSize_ActualWork();
                      }
                  
                      // The actual work method, called by worker_DoWork
                      private MemoryStream ReduceImageSize_ActualWork()
                      {
                          // Lots of code that resizes this.ImageData and assigns it to this.ResizedImageData
                      }
                  
                      private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
                      {   
                          // Async work completed - close progress dialog
                          ProgressDialog.Close();
                      }
                  }
                  

                  推荐答案

                  你不能从 BackgroundWorker 调用 ShowDialog.您必须使用 Dispatcher 要求 UI 线程执行它:

                  You can't call ShowDialog from the BackgroundWorker. You have to use the Dispatcher to ask the UI thread to execute it:

                   this.Dispatcher.BeginInvoke(new Action(() => ProgressDialog.ShowDialog()));
                  

                  BackgroundWorker的'Completed'事件是在UI线程中执行的,所以这部分应该没问题.

                  The 'Completed' event of the BackgroundWorker is executed in the UI thread, so this part should be fine.

                  这篇关于在 BackgroundWorker 运行时显示模态窗口,而不会出现 STA/MTA 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C# namespace alias - what#39;s the point?(C# 命名空间别名 - 有什么意义?)
                  Using Xpath With Default Namespace in C#(在 C# 中使用具有默认命名空间的 Xpath)
                  Generating an EDMX from a DB2 Database(从 DB2 数据库生成 EDMX)
                  IBM .NET Data Provider Connection String issue with Library List(库列表的 IBM .NET 数据提供程序连接字符串问题)
                  .NET DB2 OLEDB pre-requisites(.NET DB2 OLEDB 先决条件)
                  Referring to Code in IBM.Data.DB2 makes that Assembly Unavailable to the rest of my Solution(引用 IBM.Data.DB2 中的代码使该程序集对我的解决方案的其余部分不可用)

                        <tbody id='WG5OR'></tbody>

                    1. <tfoot id='WG5OR'></tfoot>
                      <i id='WG5OR'><tr id='WG5OR'><dt id='WG5OR'><q id='WG5OR'><span id='WG5OR'><b id='WG5OR'><form id='WG5OR'><ins id='WG5OR'></ins><ul id='WG5OR'></ul><sub id='WG5OR'></sub></form><legend id='WG5OR'></legend><bdo id='WG5OR'><pre id='WG5OR'><center id='WG5OR'></center></pre></bdo></b><th id='WG5OR'></th></span></q></dt></tr></i><div id='WG5OR'><tfoot id='WG5OR'></tfoot><dl id='WG5OR'><fieldset id='WG5OR'></fieldset></dl></div>

                      • <bdo id='WG5OR'></bdo><ul id='WG5OR'></ul>
                          • <small id='WG5OR'></small><noframes id='WG5OR'>

                            <legend id='WG5OR'><style id='WG5OR'><dir id='WG5OR'><q id='WG5OR'></q></dir></style></legend>