Thread Question

F

fniles

I am using thread (sub ThreadMain in the ClientSession class). I call it
from main application called MainForm.
Inside ThreadMain, I call a subroutine WriteLine from the MainForm.
Will WriteLine run/processed inside or outside the thread ?

Thanks.

Code from main appl:
Public Class MainForm
Private Session As ClientSession = Nothing

' 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)

' Initialize the client session object with the reference to
' this form, and the socket that was created
Session.ClientForm = Me

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

Public Sub WriteLine(ByVal Line As String)
:
end sub

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

Public Sub ThreadMain()
:
ClientForm.WriteLine(strBuffer)
 
F

fniles

Thanks.
I just want to make sure.
So, even though the code for WriteLine is inside MainForm, but since it is
called from ThreadMain, it will run inside the thread, right ?

Thanks.
 
M

Mike Lowery

Anything invoked in a Thread should run inside that thread. It's analogous to
launching a separate application.
 
F

fniles

Thank you all.
Someone else suggested to marshal the call onto the form thread, by using
the
Invoke, or BeginInvoke function.
The following code is what I do.
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")
 
M

Mike Lowery

Unless I'm missing something, it should.

fniles said:
Thanks.
I just want to make sure.
So, even though the code for WriteLine is inside MainForm, but since it is
called from ThreadMain, it will run inside the thread, right ?

Thanks.
 
B

Brian Gideon

fniles said:
Thanks.
I just want to make sure.
So, even though the code for WriteLine is inside MainForm, but since it is
called from ThreadMain, it will run inside the thread, right ?

Yes, that is correct.
 
T

Tony Proctor

For future reference, the microsoft.public.vb.general.discussion newsgroup
is not designed for VB.Net. It's for "VB Classic", i.e. VB6 and beyond

Tony Proctor
 

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