Windows Forms Topics include: windows forms, controls, components and designers

Go Back  Xtreme .NET Talk > .NET > Windows Forms > How to call public sub of form running in worker thread from main thread


Reply
 
Thread Tools Display Modes
  #1  
Old 01-25-2006, 11:38 AM
JLSEsq JLSEsq is offline
Newcomer
 
Join Date: Nov 2005
Posts: 8
JLSEsq is on a distinguished road
Default How to call public sub of form running in worker thread from main thread



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...
Code:
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...

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.

Last edited by PlausiblyDamp; 01-25-2006 at 04:41 PM. Reason: Added [vb] tags for readability
Reply With Quote
  #2  
Old 01-26-2006, 12:07 PM
tfowler tfowler is offline
Regular

Preferred language:
VB.NET
 
Join Date: Aug 2005
Location: Columbus, OH
Posts: 84
tfowler is on a distinguished road
Default Use Form.Invoke

Quote:
Originally Posted by JLSEsq
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.dotnetforums.net/showthread.php?t=95171

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

Todd
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How would the main thread knows if a Worker Thread is still running microkarl General 2 07-13-2005 03:44 PM
Worker Thread and UI thread changes qex Windows Forms 7 05-11-2005 11:32 AM
Shared Ressource? between main and thread for keeping track of status Shaitan00 General 1 02-26-2005 03:30 PM
Async Methods - Syncing with Main Form's thread... Redth General 3 02-05-2005 07:29 AM
how to return to the main thread bwells Windows Forms 2 01-27-2004 07:39 PM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe