title: How to implement a popup window with a counter
body: <p>I would like to implement a simple popup window in Windows Forms, which will show a simple timer to the user while some slow-running process is executing. The premise is simple; show to the user that something is indeed going on and the application is not frozen. Note that this slow-running process is not a loop, nor is it something that I can tap into.</p>
  
  <p>What I want is a simple popup window, showing some message along the lines "Elapsed time: x seconds", where x is incremented every second.</p>
  
  <p>The basic concept is the following:</p>
  
  <pre><code>public void test()
  {
      //Some code which does stuff
  
      //Popup window with counter
  
      //Perform long running process
  
      //Close popup window with counter
  
      //Some other code which does other stuff
  }
  </code></pre>
  
  <p>I tried to do it using various ways, including background workers, threads, and of course timers. But I did not manage to make it work as I wanted. And I would prefer not to post any of my code so as not to "lead" the responses to a specific way of doing this.</p>
  
  <p>So what would be the best way to do this work?</p>
  
  <p>Thanks.</p>
  
  <p>UPDATE:
  In reply to some comments, since I cannot paste any code in the replies section, I'm editing my original question to accomodate this. One of the implementations that I tried is to spawn the popup window in a separate thread. Although I got no runtime errors, the popup window did not refresh correctly. It indeed poped-up, but no text would show within it, and the counter would not refresh. Here's the code:</p>
  
  <pre><code>private void test()
  {
      frmProgressTimer ofrmProgressTimer = new frmProgressTimer(); //Instance of popup Form
      System.Threading.Tasks.Task loadTask = new System.Threading.Tasks.Task(() =&gt; ProgressTimer(ofrmProgressTimer));
      loadTask.Start();
  
      //Perform long running process
  
      System.Threading.Tasks.Task cwt = loadTask.ContinueWith(task =&gt; EndProgressTimer(ofrmProgressTimer));
  }
  
  private void ProgressTimer(frmProgressTimer ofrmProgressTimer)
  {
      ofrmProgressTimer.Show();
  
      ofrmProgressTimer.Invoke((Action)(() =&gt;
      {                
          ofrmProgressTimer.startTimer();
      }));            
  }
  
  private void EndProgressTimer(frmProgressTimer ofrmProgressTimer)
  {
      ofrmProgressTimer.Invoke((Action)(() =&gt;
      {
          ofrmProgressTimer.stopTimer();
          ofrmProgressTimer.Close();
      }));            
  }
  </code></pre>
  
  <p>And here's my popup form code:
      public partial class frmProgressTimer : Form
      {
          private int counter = 0;
          private Timer timer1;</p>
  
  <pre><code>    public frmProgressTimer()
      {
          InitializeComponent();
  
          timer1 = new Timer();
          timer1.Interval = 1000;
          timer1.Tick += new EventHandler(timer1_Tick);
      }
  
      public void startTimer()
      {
          timer1.Start();
      }
  
      public void stopTimer()
      {
          timer1.Stop();
      }
  
      private void timer1_Tick(object sender, EventArgs e)
      {
          counter += 1;
          labelText.Text = counter.ToString();
      }
  }
  </code></pre>