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

      <legend id='imIIs'><style id='imIIs'><dir id='imIIs'><q id='imIIs'></q></dir></style></legend>
      1. <small id='imIIs'></small><noframes id='imIIs'>

      2. 进展如何&lt;T&gt;不同于Action&lt;T&gt;?(C#)

        How is Progresslt;Tgt; different from Actionlt;Tgt; ? (C#)(进展如何lt;Tgt;不同于Actionlt;Tgt;?(C#))
          <tfoot id='R1to1'></tfoot>

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

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

                    <tbody id='R1to1'></tbody>
                  <legend id='R1to1'><style id='R1to1'><dir id='R1to1'><q id='R1to1'></q></dir></style></legend>
                • 本文介绍了进展如何&lt;T&gt;不同于Action&lt;T&gt;?(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在使用Progress<T>,想知道是否可以将其替换为Action<T>.

                  I've been using Progress<T> and wondered if it can be replaced by Action<T>.

                  在下面的代码中,使用它们中的每一个来报告进度,即 ReportWithProgress()ReportWithAction(),对我没有任何明显的影响.progressBar1 是如何增加的,字符串是如何写在输出窗口上的,它们看起来都一样.

                  In the code below, using each of them for reporting progress, i.e. ReportWithProgress() or ReportWithAction(), didn't make any noticeable difference to me. How progressBar1 increased, how the strings were written on the output window, they seemed the same.

                  // WinForm application with progressBar1
                  
                  private void HeavyIO()
                  {
                      Thread.Sleep(20); // assume heavy IO
                  }
                  
                  private async Task ReportWithProgress()
                  {
                      IProgress<int> p = new Progress<int>(i => progressBar1.Value = i);
                  
                      for (int i = 0; i <= 100; i++)
                      {
                          await Task.Run(() => HeavyIO()); 
                          Console.WriteLine("Progress : " + i);
                          p.Report(i);
                      }
                  }
                  
                  private async Task ReportWithAction()
                  {
                      var a = new Action<int>(i => progressBar1.Value = i);
                  
                      for (int i = 0; i <= 100; i++)
                      {
                          await Task.Run(() => HeavyIO());
                          Console.WriteLine("Action : " + i);
                          a(i);
                      }
                  } 
                  

                  但是 Progress<T> 不能是轮子的再发明.实施它应该是有原因的.谷歌搜索c# Progress vs Action"并没有给我太多帮助.Progress 与 Action 有何不同?

                  But Progress<T> can't be a reinvention of the wheel. There should be a reason why it was implemented. Googling "c# Progress vs Action" didn't give me much help. How is Progress different from Action?

                  推荐答案

                  从不同的线程调用 progressBar1.Value = i 会导致可怕的 跨线程操作无效"异常.另一方面,Progress 类将事件分派到 同步上下文在构建时刻捕获:

                  Calling progressBar1.Value = i from a different thread results in the dreaded "cross-thread operation not valid" exception. The Progress class, on the other hand, dispatches the event to the synchronization context captured in the moment of construction:

                  // simplified code, check reference source for actual code
                  
                  void IProgress<T>.Report(T value)
                  {
                      // post the processing to the captured sync context
                      m_synchronizationContext.Post(InvokeHandlers, value);
                  }
                  
                  private void InvokeHandlers(object state)
                  {
                      // invoke the handler passed through the constructor
                      m_handler?.Invoke((T)state);
                  
                      // invoke the ProgressChanged event handler
                      ProgressChanged?.Invoke(this, (T)state);
                  }
                  

                  这可确保对进度条、标签和其他 UI 元素的所有更新都在(一个且唯一的)GUI 线程上完成.

                  This ensures that all updates to progress bars, labels and other UI elements are done on a (one and only) GUI thread.

                  因此,只有在 UI 线程上调用的方法内实例化后台线程的 Progressoutside 才有意义:

                  So, it only makes sense to instantiate the Progress class outside of the background thread, inside a method which is called on a UI thread:

                  void Button_Click(object sender, EventArgs e)
                  {
                      // since this is a UI event, instantiating the Progress class
                      // here will capture the UI thread context
                      var progress = new Progress<int>(i => progressBar1.Value = i);
                  
                      // pass this instance to the background task
                      Task.Run(() => ReportWithProgress(progress));
                  }
                  
                  async Task ReportWithProgress(IProgress<int> p)
                  {
                      for (int i = 0; i <= 100; i++)
                      {
                          await Task.Run(() => HeavyIO());
                          Console.WriteLine("Progress : " + i);
                          p.Report(i);
                      }
                  }
                  

                  这篇关于进展如何&lt;T&gt;不同于Action&lt;T&gt;?(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  • <bdo id='7boKN'></bdo><ul id='7boKN'></ul>

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

                          <tbody id='7boKN'></tbody>
                      1. <small id='7boKN'></small><noframes id='7boKN'>

                          <legend id='7boKN'><style id='7boKN'><dir id='7boKN'><q id='7boKN'></q></dir></style></legend>

                          • <tfoot id='7boKN'></tfoot>