RE: Threading Issue
- From: "AMercer" <AMercer@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 18 May 2005 17:49:01 -0700
I understand your problem. More generally, one might have multiple threads
and each thread might have multiple outputs. Raising an event on a
particular thread sounds like a nice idea, but I dont think it can be done.
My answer to this program organization issue is as follows:
1. A class that contains a Queue property.
2. Thread-safe Enqueue and Dequeue methods (worker threads enqueue, the
calling thread dequeues)
3. What gets enqueued and dequeued is always an array of objects (I
sacrifice strong typing for flexibility). In my game, the zero'th entry is
always a string that defines what follows. Enqueue has an arg like 'ByVal
ParamArray Data() As Object'.
Dequeue is a function returning Object().
4. The class also has Form and Timer properties. If set to other than
Nothing, whenever data is enqueued, the class does an Invoke to enable the
timer for an immediate tick, and this is how the worker thread dings the main
thread. If you prefer, your main thread could just poll the queue.
5. Not essential to the concept is the fact that I actually use three
queues, flash, normal, and idle. Worker threads can enqueue to any queue,
but dequeue always gives preference to flash over normal, and normal over
idle. It is a useful gimmick for my apps, but it is not essential.
6. Total size of the class is about 60 lines of VB, maybe 100 total with
comments and whitespace.
I know this sounds like overkill given your question, but my multi-thread
programs evolved in this direction, and I am content with it. To me it is
just a bit of threading infrastructure. I also don't claim it is the best
way - it is just my way.
Finally, be advised - if your main thread is a form thread, then the 'dirty
hack' of some variant of Invoke is absolutely necessary.
.
- References:
- Threading Issue
- From: Brad Brening
- Threading Issue
- Prev by Date: Re: public ip address
- Next by Date: Re: More C# to VB
- Previous by thread: Threading Issue
- Next by thread: How to make treeview node display several column
- Index(es):
Relevant Pages
|