Thread question

F

fiefie.niles

I am using a thread. Inside the thread there is a sub ThreadMain. I am
calling the thread from the main program MainForm.
Inside the thread, if I am calling a sub (say doProcess) from the
MainForm, does it mean that doProcess is processed inside or outside
the thread ?

Code from MainForm:
Public Class MainForm
Private Session As ClientSession = Nothing
Private WorkerThread As Threading.Thread = Nothing

Public Sub ConnectMenu_Click
:
' Create a new instance of the ClientSession object and a
worker
' thread that will handle the socket I/O
Session = New ClientSession
WorkerThread = New Threading.Thread(AddressOf
Session.ThreadMain)

Session.ClientForm = Me

' Initialize the worker thread and start its execution
WorkerThread.Name = "WorkerThread"
WorkerThread.Start()

Code from the thread:
Public Class ClientSession
Public ClientForm As MainForm = Nothing

Public Sub ThreadMain()
:
ClientForm.doProcess(strBuffer) --->will this be executed inside or
outside the thread ?????
:

Thanks.
 
R

Robin Mark Tucker

If you call a method from a thread, that process is executed on that thread,
same for your process. So in your example, the thread will execute the
method on the form, from within the thread - not a good idea.

What you can do is to marshal the call onto the form thread, by using the
Invoke, or BeginInvoke function. Here is a simple page I found that
explains the basics:

http://abstractvb.com/code.asp?A=1027
 
F

fniles

Thanks.
Isn't Invoke only for changing a control in the form thread ?
Inside ClientForm.doProcess, I do call Invoke when changing a textbox value
from the form thread.
Do I also have to call doProcess using the invoke method ?

Thanks.
 
F

fniles

The following code is what I do. Pls let me know if it looks ok to you.
If I do like the following, will sub PQ be executed on the MainForm or the
Class GT Thread ?
Thank you.

Public Class MainForm
Private GSession As GT = Nothing
Private GWorkerThread As Threading.Thread = Nothing

Public Sub ConnectMenu_Click
:
' Create a new instance of the ClientSession object and a worker
' thread that will handle the socket I/O
GSession = New GSession
GWorkerThread = New Threading.Thread(AddressOf GSession.ThreadMain)


GSession.ClientForm = Me
GSession.cb = AddressOf ProcessQuote

' Initialize the worker thread and start its execution
GWorkerThread.Name = "WorkerThread"
GWorkerThread.Start()
:
end sub

Sub PQ(ByVal Packet As String, ByVal sCaller As String)

Inside the Class Thread:
Public Class GT
Public ClientForm As MainForm = Nothing
Public Delegate Sub PQCallBack(ByVal Packet As String, ByVal sCaller As
String)
Public cb As PQCallBack

Public Sub ThreadMain()
Dim d As PQCallBack

d = cb
d(strBuffer, "CMC")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top