<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 问题

        时间:2023-09-14
            <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 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 ShowDialog() 阻塞调用之前注册消息处理程序? 下一篇:OK-Cancel Dialog:处理“Enter"键按下

                  相关文章

                    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>
                    2. <small id='WG5OR'></small><noframes id='WG5OR'>

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