Communication between threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jck

    #1

    Communication between threads

    Could someone tell me how a thread can send data / post message to another
    thread in vb.net please?

    I have a main thread and a socket thread. I need a way to let the main
    thread know when my socket thread receive data.

    Thanks in advance!
  • iwdu15

    #2
    RE: Communication between threads

    do you need to ghave each on different threads? the Socket class has the
    ability to get data async so it wont freeze ur UI, then the socket and main
    thread are on the same thread, which just makes things easier
    --
    -iwdu15

    Comment

    • Jck

      #3
      RE: Communication between threads

      Do you mean I need to have a timer to check the socket receive buffer size in
      UI thread?

      "iwdu15" wrote:
      do you need to ghave each on different threads? the Socket class has the
      ability to get data async so it wont freeze ur UI, then the socket and main
      thread are on the same thread, which just makes things easier
      --
      -iwdu15

      Comment

      • Tom Shelton

        #4
        Re: Communication between threads


        iwdu15 wrote:
        do you need to ghave each on different threads? the Socket class has the
        ability to get data async so it wont freeze ur UI, then the socket and main
        thread are on the same thread, which just makes things easier
        That isn't really true. The async socket calls are still executed on a
        separate thread from the main thread - you just don't explicitly create
        that thread, it is taken from the thread pool.

        --
        Tom Shelton [MVP]

        Comment

        • Jck

          #5
          RE: Communication between threads

          I still need a way for threads to communicate with each other.

          Other than the Main and Socket threads, I will have some more threads such
          as Printer thread and Trace log thread.

          I just realize I can open a file for writing from my Main thread and let the
          socket thread use the FileNumber to do any writing. However, this problem
          solved if I can have the socket thread send/post a message to my Main thread.

          Someone, please help!!

          "Jck" wrote:
          Could someone tell me how a thread can send data / post message to another
          thread in vb.net please?
          >
          I have a main thread and a socket thread. I need a way to let the main
          thread know when my socket thread receive data.
          >
          Thanks in advance!

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Communication between threads

            Jck,

            You can use any shared variable in the main and in the worker thread to use
            as communication information.

            Be aware that you while updating need to synclock that.

            http://msdn.microsoft.com/library/de...tmSyncLock.asp

            I like for that the queue class.

            http://msdn2.microsoft.com/en-us/lib...ons.queue.aspx

            I hope this helps,

            Cor

            "Jck" <Jck@discussion s.microsoft.com schreef in bericht
            news:D497A8C9-0121-483E-92F6-172DBED175A7@mi crosoft.com...
            >I still need a way for threads to communicate with each other.
            >
            Other than the Main and Socket threads, I will have some more threads such
            as Printer thread and Trace log thread.
            >
            I just realize I can open a file for writing from my Main thread and let
            the
            socket thread use the FileNumber to do any writing. However, this problem
            solved if I can have the socket thread send/post a message to my Main
            thread.
            >
            Someone, please help!!
            >
            "Jck" wrote:
            >
            >Could someone tell me how a thread can send data / post message to
            >another
            >thread in vb.net please?
            >>
            >I have a main thread and a socket thread. I need a way to let the main
            >thread know when my socket thread receive data.
            >>
            >Thanks in advance!

            Comment

            • Brian Gideon

              #7
              Re: Communication between threads

              Messages can be sent between threads using several mechanisms. You can
              use a WaitHandle (either ManualResetEven t or AutoResetEvent) to cause a
              thread to block until another thread signals an event. You can use
              Monitor.Pulse and Monitor.Wait in much the same way. If one of your
              threads is running a Windows message loop then you can use
              Control.Invoke or Control.BeginIn voke. And of course data can be
              shared between threads by making sure the variables are accessible from
              the code that each thread executes. The variables themselves can even
              be used a signalling mechanism. It really depends on exactly how you
              want the threads to work together.

              Brian

              Jck wrote:
              Could someone tell me how a thread can send data / post message to another
              thread in vb.net please?
              >
              I have a main thread and a socket thread. I need a way to let the main
              thread know when my socket thread receive data.
              >
              Thanks in advance!

              Comment

              Working...