Thread question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fiefie.niles@gmail.com

    #1

    Thread question

    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.Threa d = Nothing

    Public Sub ConnectMenu_Cli ck
    :
    ' 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.Threa d(AddressOf
    Session.ThreadM ain)

    Session.ClientF orm = Me

    ' Initialize the worker thread and start its execution
    WorkerThread.Na me = "WorkerThre ad"
    WorkerThread.St art()

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

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

    Thanks.

  • Robin Mark Tucker

    #2
    Re: Thread question

    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:

    Explore creative outlets through pottery, music, and woodworking classes. These hands-on experiences bridge analytical rigor and artistic spontaneity, enhancing personal and professional growth.





    <fiefie.niles@g mail.com> wrote in message
    news:1149261322 .510095.76070@g 10g2000cwb.goog legroups.com...[color=blue]
    >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.Threa d = Nothing
    >
    > Public Sub ConnectMenu_Cli ck
    > :
    > ' 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.Threa d(AddressOf
    > Session.ThreadM ain)
    >
    > Session.ClientF orm = Me
    >
    > ' Initialize the worker thread and start its execution
    > WorkerThread.Na me = "WorkerThre ad"
    > WorkerThread.St art()
    >
    > Code from the thread:
    > Public Class ClientSession
    > Public ClientForm As MainForm = Nothing
    >
    > Public Sub ThreadMain()
    > :
    > ClientForm.doPr ocess(strBuffer ) --->will this be executed inside or
    > outside the thread ?????
    > :
    >
    > Thanks.
    >[/color]


    Comment

    • fniles

      #3
      Re: Thread question

      Thanks.
      Isn't Invoke only for changing a control in the form thread ?
      Inside ClientForm.doPr ocess, 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.

      "Robin Mark Tucker" <robintuckerhom e@removehotmail .comremove> wrote in
      message news:e5pojp$djj $1$8302bc10@new s.demon.co.uk.. .[color=blue]
      > 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
      >
      >
      >
      >
      > <fiefie.niles@g mail.com> wrote in message
      > news:1149261322 .510095.76070@g 10g2000cwb.goog legroups.com...[color=green]
      >>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.Threa d = Nothing
      >>
      >> Public Sub ConnectMenu_Cli ck
      >> :
      >> ' 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.Threa d(AddressOf
      >> Session.ThreadM ain)
      >>
      >> Session.ClientF orm = Me
      >>
      >> ' Initialize the worker thread and start its execution
      >> WorkerThread.Na me = "WorkerThre ad"
      >> WorkerThread.St art()
      >>
      >> Code from the thread:
      >> Public Class ClientSession
      >> Public ClientForm As MainForm = Nothing
      >>
      >> Public Sub ThreadMain()
      >> :
      >> ClientForm.doPr ocess(strBuffer ) --->will this be executed inside or
      >> outside the thread ?????
      >> :
      >>
      >> Thanks.
      >>[/color]
      >
      >[/color]


      Comment

      • fniles

        #4
        Re: Thread question

        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.Threa d = Nothing

        Public Sub ConnectMenu_Cli ck
        :
        ' 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.Threa d(AddressOf GSession.Thread Main)


        GSession.Client Form = Me
        GSession.cb = AddressOf ProcessQuote

        ' Initialize the worker thread and start its execution
        GWorkerThread.N ame = "WorkerThre ad"
        GWorkerThread.S tart()
        :
        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(ByVa l Packet As String, ByVal sCaller As
        String)
        Public cb As PQCallBack

        Public Sub ThreadMain()
        Dim d As PQCallBack

        d = cb
        d(strBuffer, "CMC")

        "Robin Mark Tucker" <robintuckerhom e@removehotmail .comremove> wrote in
        message news:e5pojp$djj $1$8302bc10@new s.demon.co.uk.. .[color=blue]
        > 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
        >
        >
        >
        >
        > <fiefie.niles@g mail.com> wrote in message
        > news:1149261322 .510095.76070@g 10g2000cwb.goog legroups.com...[color=green]
        >>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.Threa d = Nothing
        >>
        >> Public Sub ConnectMenu_Cli ck
        >> :
        >> ' 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.Threa d(AddressOf
        >> Session.ThreadM ain)
        >>
        >> Session.ClientF orm = Me
        >>
        >> ' Initialize the worker thread and start its execution
        >> WorkerThread.Na me = "WorkerThre ad"
        >> WorkerThread.St art()
        >>
        >> Code from the thread:
        >> Public Class ClientSession
        >> Public ClientForm As MainForm = Nothing
        >>
        >> Public Sub ThreadMain()
        >> :
        >> ClientForm.doPr ocess(strBuffer ) --->will this be executed inside or
        >> outside the thread ?????
        >> :
        >>
        >> Thanks.
        >>[/color]
        >
        >[/color]


        Comment

        Working...