Threading in .Net...

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

    #1

    Threading in .Net...

    Hello Everyone,

    I have to write a program that uses multiple threads. Simply, this is
    what I want to do.

    1) The parent thread will spawn at least two child threads.
    2) Child Thread 1 will monitor data from a source and if the
    conditions are TRUE, it would post a message to the top of a message
    queue. Then alert Child Thread 2 that there is a message in the queue.
    3) Child Thread 2 would pop the tail of the message queue until all
    the messages have been processed and reported to the user.

    Normally in C++ or Delphi, I would use a Critical Section variables
    around the global memory structure that would prevent more than one
    thread accessing the data in memory at the same time and use windows
    postmessage events to notify the thread that it needs to look at the
    message queue. Unfortunately, I can not use C++, Dephi or C#. I have
    to use VB .Net. The good thing about .Net is the fact that it supports
    threading. It is very easy to create threads and start them in .Net.
    However, the way it provides messaging between threads is a little
    confusing to me or I might be just over analysing it. I could use
    monitor like the critical section variables to protect the global
    memory structure, but how I do I notify Child Thread 2 that there is a
    message waiting in the Queue.

    What is the recomended way to do this in .Net?

    Thank you in advance...

  • Mehdi

    #2
    Re: Threading in .Net...

    On 6 Jun 2006 12:12:57 -0700, CirclesTraveled wrote:
    [color=blue]
    > It is very easy to create threads and start them in .Net.
    > However, the way it provides messaging between threads is a little
    > confusing to me or I might be just over analysing it. I could use
    > monitor like the critical section variables to protect the global
    > memory structure, but how I do I notify Child Thread 2 that there is a
    > message waiting in the Queue.
    >
    > What is the recomended way to do this in .Net?[/color]

    Use a ManualResetEven t. Have thread 2 wait on it. In thread 1, when a new
    item is placed on the queue, set the event. This will Cause thread 2 to
    wake up. Then thread 2 can do it's job and process all the items in the
    queue. When it's done it can reset the ManualResetEven t and wait until
    woken up again.

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Threading in .Net...

      Circles,

      I would use two threads.
      The parent thread and the worker thread.

      I assume that you are using the queue class.

      You can thrown an event in the workerthread to tell that you have placed
      something in the queue.

      Another approach and even simpler is just a timerevent in the mainthread in
      what you look if the queue is empty or not, and if not, just start
      processing it until it is empty.

      Be aware that you synclock the queue before you something put in (from the
      worker thread) or get from (from the mainthread) it.

      Don't forget to look in the queue (if that is needed) at closing from the
      program.

      I hope this helps,

      Cor

      "CirclesTravele d" <hicksd@ecg-inc.com> schreef in bericht
      news:1149621177 .500865.273710@ j55g2000cwa.goo glegroups.com.. .[color=blue]
      > Hello Everyone,
      >
      > I have to write a program that uses multiple threads. Simply, this is
      > what I want to do.
      >
      > 1) The parent thread will spawn at least two child threads.
      > 2) Child Thread 1 will monitor data from a source and if the
      > conditions are TRUE, it would post a message to the top of a message
      > queue. Then alert Child Thread 2 that there is a message in the queue.
      > 3) Child Thread 2 would pop the tail of the message queue until all
      > the messages have been processed and reported to the user.
      >
      > Normally in C++ or Delphi, I would use a Critical Section variables
      > around the global memory structure that would prevent more than one
      > thread accessing the data in memory at the same time and use windows
      > postmessage events to notify the thread that it needs to look at the
      > message queue. Unfortunately, I can not use C++, Dephi or C#. I have
      > to use VB .Net. The good thing about .Net is the fact that it supports
      > threading. It is very easy to create threads and start them in .Net.
      > However, the way it provides messaging between threads is a little
      > confusing to me or I might be just over analysing it. I could use
      > monitor like the critical section variables to protect the global
      > memory structure, but how I do I notify Child Thread 2 that there is a
      > message waiting in the Queue.
      >
      > What is the recomended way to do this in .Net?
      >
      > Thank you in advance...
      >[/color]


      Comment

      • Larry Lard

        #4
        Re: Threading in .Net...


        CirclesTraveled wrote:[color=blue]
        > Hello Everyone,
        >
        > I have to write a program that uses multiple threads. Simply, this is
        > what I want to do.
        >
        > 1) The parent thread will spawn at least two child threads.
        > 2) Child Thread 1 will monitor data from a source and if the
        > conditions are TRUE, it would post a message to the top of a message
        > queue. Then alert Child Thread 2 that there is a message in the queue.
        > 3) Child Thread 2 would pop the tail of the message queue until all
        > the messages have been processed and reported to the user.[/color]
        ....[color=blue]
        > What is the recomended way to do this in .Net?[/color]

        This is known as a 'producer-consumer queue', and I know of at least
        two implementations published on the web:

        Jon Skeet's:
        <http://www.yoda.arachs ys.com/csharp/threads/deadlocks.shtml > as part
        of his excellent pages on threading in general. In C#, but you should
        be able to understand it.

        Joe Albahari's: <http://www.albahari.co m/threading/index.html>, in the
        'Wait and Pulse' section. Also in C#, but with pretty colors too! :)

        Since it sounds like you know how to do this stuff in C++, there
        shouldn't be any major surprises awaiting you in the .NET threading
        model.

        --
        Larry Lard
        Replies to group please

        Comment

        • CirclesTraveled

          #5
          Re: Threading in .Net...

          Thank you all for you suggestions. I will be exploring all
          possibilities. I will let everyone know what happens... Again, Thank
          you...

          Comment

          Working...