Jump to content
Xtreme .Net Talk

How to call public sub of form running in worker thread from main thread


Recommended Posts

Posted (edited)

I need help calling a public sub on a form running on a worker thread from the main thread. I think I need to use a delegate, but I've no idea how to declare or call a delegate.

 

I've created a form with one simple sub...

Public Sub Set_Prompt(ByVal Use_Prompt As String)

       Me.Prompt_La.Text = Use_Prompt
End Sub

I create a worker thread which shows the form, and I can close the form from the main thread using this code...

 

Imports System
Imports System.Threading

Module WaitLib

   Dim WaitMsg_Win As WaitMsg = Nothing
   Dim Msg_Thread As Thread
   Dim Thread_Running As Boolean = False
   Dim Use_Msg As String

   Private Sub Show_WaitMsg()

       WaitMsg_Win = New WaitMsg
       WaitMsg_Win.Set_Prompt(Use_Msg)
       WaitMsg_Win.ShowDialog()
       While (True)
           Thread.Sleep(100)
       End While
   End Sub

   Public Sub Display_Wait_Message(ByVal Wait_Msg As String)

       Use_Msg = Wait_Msg

       If (Not Thread_Running) Then
           Try
               Msg_Thread = New Thread(AddressOf Show_WaitMsg)
               Msg_Thread.IsBackground = True
               Msg_Thread.Priority = ThreadPriority.BelowNormal
               Msg_Thread.Name = "WaitMsg Thread"
               Msg_Thread.Start()
               Thread_Running = True
           Catch ex As Exception
           End Try
       Else
           '
           ' Need to call WaitMsg_Win.Set_Prompt(Use_Msg)
           '
       End If
   End Sub

   Public Sub End_Wait_Message()

       If (Thread_Running) Then
           Try
               Msg_Thread.Abort()
               Msg_Thread.Join()
               WaitMsg_Win.Close()
               WaitMsg_Win = Nothing
               Thread_Running = False
           Catch Err_Ex As Exception
               Call Error_Proc(Err_Ex)
           End Try
       End If
   End Sub

End Module

Thanks in advance for all help and suggestions.

Edited by PlausiblyDamp
Posted

Use Form.Invoke

 

I need help calling a public sub on a form running on a worker thread from the main thread. I think I need to use a delegate, but I've no idea how to declare or call a delegate.

You will need to use the Invoke method of the Form to run a Sub on the main thread. You can us InvokeRequired to check if you are on the main thread.

 

Check out the following thread:

http://www.xtremedotnettalk.com/showthread.php?t=95171

 

It talks about the interaction between a secondary thread and a form.

 

Todd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...