title: How do I show busy window when button click in winform apps
body: <p>My main form has one button. When the button is clicked, I  populate a grid. Here is my button click routine</p>
  
  <pre><code>private void btnSearch_Click(object sender, EventArgs e)
  {
      if (chkExcludeCallCust.Checked)
      {
          if (chkEnable.Checked)
              RangeExclude = 1;
          else
              RangeExclude = 0;
      }
  
      new Thread(() =&gt;
      {
          lock (thisLock)
          {
              Search();
          }
      }).Start();
  
  }
  </code></pre>
  
  <p>A thread is called when the user clicks on the search button. From that thread I fetch data from the database and populate the grid.</p>
  
  <p>So here is my search routine:</p>
  
  <pre><code>private void Search()
  {
      DataSet ds = null;
  
      if (wfrm.InvokeRequired)
      {
          wfrm.Invoke(new MethodInvoker(delegate
          {
              wfrm.Show();
              wfrm.Refresh();
              Application.DoEvents();
          }));
      }
      else
      {
          wfrm.Show();
          wfrm.Refresh();
          Application.DoEvents();
      }
  
      if (this.InvokeRequired)
      {
          this.Invoke(new MethodInvoker(delegate { 
              this.Cursor = Cursors.WaitCursor;
              btnSearch.Enabled = false;
          }));
      }
      else
      {
          this.Cursor = Cursors.WaitCursor;
          btnSearch.Enabled = false;
      }
  
      if (!PingTest())
      {
          if (this.InvokeRequired)
          {
              this.Invoke(new MethodInvoker(delegate
              {
                  this.Cursor = Cursors.Default;
                  MessageBox.Show("VPN disconnected. Please connect and try again.", "DebtorCallerAssistance");
                  return;
              }));
          }
          else
          {
              this.Cursor = Cursors.Default;
              MessageBox.Show("VPN disconnected. Please connect and try again.", "DebtorCallerAssistance");
              return;
          }
      }
  
      if (chkExcludeCallCust.InvokeRequired)
      {
          chkExcludeCallCust.Invoke(new MethodInvoker(delegate
          {
              DataLayer.SetConnectionString(_country);
              ds = LoadData(dtfrom.Value.ToString("yyyyMMdd"), dtto.Value.ToString("yyyyMMdd"), (chkExcludeCallCust.Checked ? 1 : 0), txtSearch.Text, 1, Pager.PageSize, false);
          }));
      }
      else
      {
          DataLayer.SetConnectionString(_country);
          ds = LoadData(dtfrom.Value.ToString("yyyyMMdd"), dtto.Value.ToString("yyyyMMdd"), (chkExcludeCallCust.Checked ? 1 : 0), txtSearch.Text, 1, Pager.PageSize, false);
      }
  
      if (ds.Tables.Count &gt; 0)
      {
          if (ds.Tables[0] != null)
          {
              dtExport = ds.Tables[0];
          }
      }
  
      if (outlookGrid1.InvokeRequired)
      {
          outlookGrid1.Invoke(new MethodInvoker(delegate
          {
              outlookGrid1.BindData(ds, "data");
              View = "BoundInvoices";
              DataGridViewCellEventArgs evt = new DataGridViewCellEventArgs(2, -1);
              object sender = null;
              //outlookGrid1_CellClick(sender, evt);
          }));
      }
      else
      {
          //outlookGrid1.DataSource = ds.Tables[0];
          outlookGrid1.BindData(ds, "data");
          View = "BoundInvoices";
          DataGridViewCellEventArgs evt = new DataGridViewCellEventArgs(2, -1);
          object sender = null;
          //outlookGrid1_CellClick(sender, evt);
      }
  
      if (wfrm.InvokeRequired)
      {
          wfrm.Invoke(new MethodInvoker(delegate
          {
              wfrm.Hide();
          }));
      }
      else
      {
          wfrm.Hide();
      }
  
      if (this.InvokeRequired)
      {
          this.Invoke(new MethodInvoker(delegate { 
          this.Cursor = Cursors.Default;
          btnSearch.Enabled = true;
          }));
      }
      else
      {
          btnSearch.Enabled = true;
          this.Cursor = Cursors.Default;
      }
  }
  </code></pre>
  
  <p>At form load, I instantiate a borderless windows like:</p>
  
  <pre><code>WaitForm wfrm = null;
  public Feedback(string country)
  {
      InitializeComponent();
      wfrm = new WaitForm();
  }
  </code></pre>
  
  <p>I also set a  few more properties for this window like:</p>
  
  <pre><code>startposition = CenterScreen
  showinicon=false
  showintaskbar=false
  formboderstyle=none
  </code></pre>
  
  <p><code>WaitForm</code> is shown only the first time I click the search button  I am not able to understand why <code>WaitForm</code> is not shown if the search button is clicked a second time.</p>
  
  <p>Another issue is that there is a <code>PictureBox</code> on  <code>WaitForm</code> which has been assigned an animated  GIF. The first time <code>WaitForm</code> is shown the animation is not playing. Why animation isn't the animation playing when I show the WinForm?</p>
  
  <p>Please tell me what I need to change in code in order to show the <code>WaitForm</code> whenever I click on the search button. I'd also like to know how I can make the GIF in <code>WaitForm</code>'s <code>PictureBox</code> animate.</p>