Results 1 to 12 of 12

Thread: [RESOLVED] Raising events on different threads

Threaded View

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Resolved [RESOLVED] Raising events on different threads

    This is related to an early thread I started, but my design and understanding of the problem has changed enough that I figured I'd start something new:

    I have a class that accepts UDP data and puts it in a standardized way into a container class that has been designed to be thread safe. The class that wraps the UDP operations takes the datagrams, packages them into a standardized packaging class, and adds the class to a queue in the receiver class. A different process on a different thread takes items off the queue as it gets the opportunity, or as items arrive.

    This could be done via polling, because I am currently unsure which process will proceed faster. I expect that the UDP thread will be slower, but much more will be happening on the other thread, so it may not read with great frequency.....or maybe it will.

    This poses a bit of a problem for me. If datagrams are received, and the data queued up fast enough that the queue can grow, then polling is a reasonable way to handle data coming in. If, however, the main thread is spending lots of time sitting around waiting for data to arrive in the queue, then polling would prevent an efficient use of that time. I suspect that the latter case will be FAR more typical. Therefore, I think it would be best if the receiver class that wraps the queue raise an event on the main thread when something new is added to the queue. However, the adding will happen on the UDP thread, not the main thread.

    So my question is: What is the best way for an event to be raised on the main thread when something happens in a different thread. I can see one way to do it, but I'm sure it wouldn't be the best way: Have a sub in the receiver class that does nothing but raise the event, and Invoke that sub from the other thread. Alternatively, I could Invoke() a sub that reads from the queue, but that would force the main thread, which is less desirable than just raising an event and letting the main thread do as it pleased with the event.

    EDIT: Slight update on the description of the problem: I had some goofy stuff going on with a Singleton class contained in a Singleton class, so I combined the two into one. Now, the class that holds the UDP thread also holds the queue, but the class itself is a Singleton. When the UDP receiving thread adds something to the queue, the class that contains the UDP thread needs to raise an event on the main thread.
    Last edited by Shaggy Hiker; Jul 2nd, 2007 at 11:10 AM.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width