|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
thread problemHello,
if habe the class "receiver" which reads data out of a stream. that the application doesn't get blocket the class starts the function "readData" in its own thread. but I want the function for each incoming data to call a function in in the receiver thread, not in its own! P.S. the receiver class doesn't run in an UI thread, so Control.Invoke doesn't work! hope, you understand...I'm german :) *** Sent via Developersdex http://www.developersdex.com *** It sounds like you're after a producer/consumer queue.
See http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml (from half way down the page). Jon I think, this is not, what I'm looking for.
example: class receiver { private delegate void StartDelegate(some params); private StartDelegate Start; public receiver() { Start = new StartDelegate(readData); Start.BeginInvoke(some params,null,null); } private void Callback(param) { //work with the data } private void readData(some params) { while(true) { //here is the main stuff to read the data //is it possible to call the callback in the parent //thread?? Callback(param); } } } *** Sent via Developersdex http://www.developersdex.com *** > //is it possible to call the callback in the parent The "parent thread" would have to be waiting for you to ask it to do> //thread?? something. I very much suspect that you do want at least one producer/consumer queue, possibly more. Jon yes, but how do I signal the parent thread, that there is data?
when I run a loop in it, which checks, if there is any, it get blocket, doesn't it? I think of a way, the worker thread fires up an event in the main thread! like it works in UI thread. with Control.Invoke... there I invoke a method in another thread... *** Sent via Developersdex http://www.developersdex.com *** > yes, but how do I signal the parent thread, that there is data? By adding something to the queue - that pulses the monitorappropriately. > when I run a loop in it, which checks, if there is any, it get blocket, Yes. So you want a thread which is just waiting for those events. If> doesn't it? you're doing other things in the thread which is meant to react to the events, it's not going to react until it notices the event. > I think of a way, the worker thread fires up an event in the main All that effectively does is put a delegate in a queue - which the UI> thread! > like it works in UI thread. with Control.Invoke... > there I invoke a method in another thread... thread picks up and runs. The message pump is effectively a producer/consumer queue. Jon > All that effectively does is put a delegate in a queue - which the UI yes, but what I'm not understand is, how does the thread knows when there is > thread picks up and runs. The message pump is effectively a > producer/consumer queue. something in the queue... perhaps some commented example, when this is not to much. dirk Dirk Reske <_Freak***@gmx.net> wrote:
> > All that effectively does is put a delegate in a queue - which the UI Using Monitor.Wait. Please reread the article I linked to earlier - it > > thread picks up and runs. The message pump is effectively a > > producer/consumer queue. > > yes, but what I'm not understand is, how does the thread knows when there is > something in the queue... > perhaps some commented example, when this is not to much. explains it all in detail. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too I think, I've understood now...
the "main" thread is the consumer....it wait's for data from the worker thread... the worker thread reads the data...and signals the main thread, wenn data is available! right? but while the time, the main thread waits for this signal...it gets blocked! and this is the thing I don't want! Show quoteHide quote "Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag news:MPG.1d93b7e68bb7d45898c716@msnews.microsoft.com... > Dirk Reske <_Freak***@gmx.net> wrote: >> > All that effectively does is put a delegate in a queue - which the UI >> > thread picks up and runs. The message pump is effectively a >> > producer/consumer queue. >> >> yes, but what I'm not understand is, how does the thread knows when there >> is >> something in the queue... >> perhaps some commented example, when this is not to much. > > Using Monitor.Wait. Please reread the article I linked to earlier - it > explains it all in detail. > > -- > Jon Skeet - <sk***@pobox.com> > http://www.pobox.com/~skeet > If replying to the group, please do not mail me too Dirk Reske <_Freak***@gmx.net> wrote:
> I think, I've understood now... That's exactly right.> > the "main" thread is the consumer....it wait's for data from the worker > thread... > the worker thread reads the data...and signals the main thread, wenn data is > available! > right? > but while the time, the main thread waits for this signal...it gets blocked! Then you need more threads - or you need to make the main thread only > and this is the thing I don't want! process the data when it's "free". (That's easy enough to do, but a somewhat different question.) -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Other interesting topics
running c# executable....
How can a parent know if a child control chages Saving TIF Image from the Internet Questions on DLinq [OT?] Who has .NET 1.1 and doesn't know it? How to Cancel a "non cancellable" Event project level scope declaration in C# .NET 2.0: code access security / authentication Enum vs Constants performance Problem when executing batch file with HelloWorld.exe %1 |
|||||||||||||||||||||||