tags:

views:

21

answers:

1

I have a small form that notifies the user on the completion of events (such as SMO restore).

I want this form to appear from various sources (such as the below SMO Restore complete event) so I guess I need to create a new thread before creating the form? As it could be called from outside the UI thread. (I also need to pass a string to this form)

The child form fades in out using a timer + Opacity.

What am I doing wrong here?

Private Sub CompleteEventHandler(ByVal sender As Object, ByVal e As Microsoft.SqlServer.Management.Common.ServerMessageEventArgs)
   MyThread = New System.Threading.Thread(AddressOf DoStuff)
    MyThread.Start("meh")
End Sub


Private Delegate Sub DoStuffDelegate(ByVal MsgString As String)
Private Sub DoStuff(ByVal MsgString As String)
    If Me.InvokeRequired Then
        Me.Invoke(New DoStuffDelegate(AddressOf DoStuff))
    Else
        Dim TempMessage As New frmNotification
        TempMessage.lblMessage.Text = MsgString
        TempMessage.Show()
    End If
End Sub
A: 

Don't start a new thread, there's no point since you're already running on another thread and InvokeRequired will always be True. The mistake is that you call Me.Invoke() but forget to pass the "MsgString" argument. You'll also want to use Me.BeginInvoke(), no need to wait. Thus:

Private Sub CompleteEventHandler(ByVal sender As Object, ByVal e As EventArgs)
    Me.BeginInvoke(New DoStuffDelegate(AddressOf DoStuff), "meh")
End Sub

Private Sub DoStuff(ByVal MsgString As String)
    Dim TempMessage As New frmNotification
    TempMessage.lblMessage.Text = MsgString
    TempMessage.Show()
End Sub
Hans Passant
Thanks Hans, great help.
madlan