title: Winforms updates with high performance
body: <p>Let me setup this question with some background information, we have a long running process which will be generating data in a Windows Form. So, obviously some form of multi-threading is going to be needed to keep the form responsive. But, we also have the requirement that the form updates as many times per second while still remaining responsive.</p>
  
  <p>Here is a simple test example using background worker thread:</p>
  
  <pre><code>void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          int reportValue = (int)e.UserState;
          label1.Text = reportValue;
          //We can put this.Refresh() here to force repaint which gives us high repaints but we lose
          //all other responsiveness with the control
  
      }
  
      void bw_DoWork(object sender, DoWorkEventArgs e)
      {
              for (int x = 0; x &lt; 100000; x++)
              {     
                //We could put Thread.Sleep here but we won't get highest performance updates
                  bw.ReportProgress(0, x);                    
              }
      }
  </code></pre>
  
  <p>Please see the comments in the code. Also, please don't question why I want this. The question is simple, how do we achieve the highest fidelity (most repaints) in updating the form while maintaining responsiveness? Forcing the repaint does give us updates but we don't process windows messages. </p>
  
  <p>I have also try placing DoEvents but that produces stack overflow. What I need is some way to say, "process any windows messages if you haven't lately". I can see also that maybe a slightly different pattern is needed to achieve this.</p>
  
  <p>It seems we need to handle a few issues:</p>
  
  <ol>
  <li>Updating the Form through the non UI thread. There are quite a few solution to this problem such as invoke, synchronization context, background worker pattern.</li>
  <li>The second problem is flooding the Form with too many updates which blocks the message processing and this is the issue around which my question really concerns. In most examples, this is handles trivially by slowing down the requests with an arbitrary wait or only updating every X%. Neither of these solutions are approriate for real-world applications nor do they meet the maximum update while responsive criteria.</li>
  </ol>
  
  <p>Some of my initial ideas on how to handle this:</p>
  
  <ol>
  <li>Queue the items in the background worker and then dispatch them in a UI thread. This will ensure every item is painted but will result in lag which we don't want.</li>
  <li>Perhaps use TPL</li>
  <li>Perhaps use a timer in the UI thread to specify a refresh value. In this way, we can grab the data at the fastest rate that we can process. It will require accessing/sharing data across threads.</li>
  </ol>
  
  <hr>
  
  <p>Update, I've updated to use a Timer to read a shared variable with the Background worker thread updates. Now for some reason, this method produces a good form response and also allows the background worker to update about 1,000x as fast. But, interestingly it only 1 millisecond accurate. </p>
  
  <p>So we should be able to change the pattern to read the current time and call the updates from the bw thread without the need for the timer.</p>
  
  <p>Here is the new pattern:</p>
  
  <pre><code>//Timer setup
  {
              RefreshTimer.SynchronizingObject = this;
              RefreshTimer.Elapsed += RefreshTimer_Elapsed;
              RefreshTimer.AutoReset = true;
              RefreshTimer.Start();
  }           
  
       void bw_DoWork(object sender, DoWorkEventArgs e)
              {
                      for (int x = 0; x &lt; 1000000000; x++)
                      {                    
                         //bw.ReportProgress(0, x);                    
                         //mUiContext.Post(UpdateLabel, x);
                          SharedX = x;
                      }
              }
  
          void RefreshTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
          {
              label1.Text = SharedX.ToString();
          }
  </code></pre>
  
  <p><strong>Update</strong> And here we have the new solution that doesn't require the timer and doesn't block the thread! We achieve a high performance in calculations and fidelity on the updates with this pattern. Unfortunately, ticks TickCount is only 1 MS accurate, however we can run a batch of X updates per MS to get faster then 1 MS timing.</p>
  
  <pre><code>   void bw_DoWork(object sender, DoWorkEventArgs e)
      {
              long lastTickCount = Environment.TickCount;                
              for (int x = 0; x &lt; 1000000000; x++)
              {
                  if (Environment.TickCount - lastTickCount &gt; 1)
                  {
                      bw.ReportProgress(0, x);
                      lastTickCount = Environment.TickCount;
                  }                 
              }
      }
  </code></pre>