(VB.Net, framework 2.0) Passing data to child threads - how?

By AdeV73 ·
Imagine, if you will, a vb.net object which is handed a lump of data (in text format); which reads a few database tables, does a bit of internal state management, and 1s +/- 0.5s later spits out some XML which will later be sent out across the network.

Now imagine 20 of those objects. Each one takes the same input data, but will output subtly (sometimes wildly) different results depending on the database contents & internal state.

Currently, each object is given its data in turn, and the resulting XML is stored away in a variable. Once the loop is complete, the resulting XML is compressed and sent across the network. Usually, the loop is pretty quick, but obviously the more objects, the slower it gets, in a linear relationship.

What I would like to do is; to split each object out onto its own thread. Each thread would be simultaneously passed the same input data, and left to its own devices. Instead of passing the XML back to the caller, it will be compressed and transmitted by the object itself.

Where I am having brain-fade, is trying to work out how to send the data to each thread... Ideally, I'd like to be able to call a sub, e.g. SendDataToThreads(myData as string) which places the data into each thread (or, maybe, into some shared memory that all threads can access), and then triggered an event in each thread that causes it to do its thing. Depending on the source, data could arrive every fraction of a second, through to once every 10 or even 30 seconds. Ideally, the subthreads would (upon receiving a "go!" command), just do their thang until they finish, then start looking for updates.

Any thoughts? All of the examples I've seen are designed to allow child threads to talk back to the main thread; which is basically the opposite of what I'm after...

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Reponse To Answer

by AdeV73 In reply to Here you go

Thanks, that allows me to start a thread with some data; however, I want the thread to stay alive after its initial run, because it's maintaining an ongoing state, and to be able to inject new data into it (and its siblings) - ideally simultaneously.

I had wondered about using internal UDP broadcasts, but getting each client to listen on the same port is, as far as I am aware, not possible. Every example of inter-process comms has been slaves-back-to-master; no master-to-slaves.

Collapse -

Why does it need to maintain a state inside the thread?

by Tony Hopkinson In reply to (VB.Net, framework 2.0) P ...

Pass in a state, and the data to process, get the result and the new state, cache the state, return the result. When you get the next request, dig the state out of the cache, pass it in with the data.
Avoid and evade, the gains you get from having thread already created, will be utterly rubbed out by them all sitting there looking to see If they have something to do.
Have a look at how to use threadpool, you'll soon see a way of avoiding this ratsnest.

Collapse -

Reponse To Answer

by AdeV73 In reply to Why does it need to maint ...

The threads will be running continuously on the background. If no new data is injected by the time the state is due to change; the state will change anyway & the thread will output new data. This occurs continuously on a loop. A thread may have 1, 2, 5 or 10 potential states. At any time, new data can be injected; this may immediately affect the thread's output; or, it may just change internal data which is output at a later state...

I do all this now, except the objects are changed only on new data (or an internal timer firing, if no new data arrives after a set time), and they're all changed at once. I'd like to make them autonomous so I don't have to deal with the delay in the main loop.

Collapse -

Reponse To Answer

by Tony Hopkinson In reply to Why does it need to maint ...

Still no need to be continually running, but if you insist move the control of the threads into an other thread, that can do your loop, internal timer etc, create teh subthreads, kills them etc.

Related Discussions

Related Forums