title: Thread-Safe Hide form
body: <p>I'm trying to make a "loading..." form in a DLL with a simple animation using a BackgroundWorker (<strong>netMessageInWait</strong>).</p>
  
  <p>I have a <strong>Show</strong> function that load the form, set all texts and run the BackgroundWorker</p>
  
  <pre><code>Public Sub Show(ByVal WorkingArea As Rectangle, ByVal Title As String, ByVal Message As String)
       With wait
          If (wait.InvokeRequired) Then
              wait.Invoke(Sub()
                              With .lblTitle
                                  .Text = Title
                              End With
                              With .lblMessage
                                  .Text = Message
                              End With
                          End Sub)
          Else
              With .lblTitle
                  .Text = Title
              End With
              With .lblMessage
                  .Text = Message
              End With
          End If
      End With
  
      If (Not backWorker.IsBusy) Then
          Dim args As New ShowArgumentType
          args.WorkingArea = WorkingArea
          args.Title = Title
          args.Message = Message
  
          backWorker.WorkerSupportsCancellation = True
          backWorker.RunWorkerAsync(args)
      End If    
  End Sub
  </code></pre>
  
  <p>And the <strong>Clear</strong> function that stop the BackgroundWorker</p>
  
  <pre><code>Public Sub ClearMessage()
      Try
          CancelWork = True
          backWorker.CancelAsync()
  
      Catch ex As Exception
          Debug.Print(ex.Message)
      End Try
  End Sub
  </code></pre>
  
  <p>The <strong>BackgroundWorker_DoWork()</strong> that show fisically the form and wait for the <strong>ClearMessage</strong></p>
  
  <pre><code>Private Sub backWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backWorker.DoWork
      Dim args As ShowArgumentType = DirectCast(e.Argument, ShowArgumentType)
  
      wait.Show()
  
      While True
          Application.DoEvents()
          If (CancelWork Or backWorker.CancellationPending) Then
              Exit While
          End If
      End While
  
  End Sub
  </code></pre>
  
  <p>And then the <strong>BackgroundWorker_RunWorkerCompleted()</strong> that Hide the form</p>
  
  <pre><code>Private Sub backWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles backWorker.RunWorkerCompleted
      CancelWork = False
  
      Call ShowHideForm_ThreadSafe(wait, False) ' HERE THE PROBLEM
  
      Debug.Print("Work completed")
  End Sub
  </code></pre>
  
  <p>Ok...wath I'm not understanding is why I try to hide the form with the function below the program freez at the BeginInvoke...</p>
  
  <pre><code>Private Sub ShowHideForm_ThreadSafe(ByVal pform As Form, _
                                      ByVal pshow As Boolean)
      If pform.InvokeRequired Then
          Dim MyDelegate As New ShowHideForm_Delegate(AddressOf ShowHideForm_ThreadSafe)
  
  
  
          pform.BeginInvoke(MyDelegate, New Object() {[pform], [pshow]})
  
  
      Else
          If (pshow) Then
              pform.Show()
          Else
              pform.Hide()
          End If
      End If
  
  End Sub
  </code></pre>
  
  <p>Please help me! How can I solve this issue?
  Thanks!</p>