Re: Thread Communication in C#



If I use ManualResetEvent, I still need some way to pass data between the
two threads. I guess I could create a queue for each thread but I'm not
sure how to pass the reference to the queue to the sub thread at start. I
thought about making it a static queue but the data must be directed to the
right thread. Any thoughts on how to get past this hurdle? Thanks for your
help.

"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:ezkoxzrGGHA.2652@xxxxxxxxxxxxxxxxxxxxxxx
> Jayme,
>
> Well, if you want a completely managed solution, I would use a
> ManualResetEvent, which is tailored for the thread. This will work only
> if you have a handful of messages you send to each thread (start, stop,
> for example), because you would have to send a different event for each
> message, and wait on those.
>
> You can also make the same calls to GetMessage and PostThreadMessage if
> you wish, making the calls through the P/Invoke layer.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> "Jayme Pechan" <jayme.pechan@xxxxxxxxxxxxx> wrote in message
> news:%23T7SpvrGGHA.1312@xxxxxxxxxxxxxxxxxxxxxxx
>> I've been working on porting an application to C# that was previously
>> written in C++. This application is a windows service application so
>> there is no user interface. I have a number of worker threads in C++
>> that create components and wait for messages from the main thread to
>> process work. This was all done using the PostThreadMessage and
>> GetMessage. I recall that in Unix I used to do this kind of thing with
>> Pipes but I can't figure out how to do this in C#. I need the thread to
>> block until the main thread sends it data to process and that is what the
>> Pipes and GetMessage both did. Any idea what is the correct way to
>> duplicate this behavior in C# managed code? Thanks.
>>
>
>


.



Relevant Pages

  • Re: Thread Communication in C#
    ... > sure how to pass the reference to the queue to the sub thread at start. ... >> You can also make the same calls to GetMessage and PostThreadMessage ... >>> the Pipes and GetMessage both did. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Mutex misunderstanding
    ... ManualResetEvent instead. ... private Queue myQ = new Queue; ... lock ... Task RemoveFromQ() { ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Threading in .Net...
    ... the way it provides messaging between threads is a little ... monitor like the critical section variables to protect the global ... item is placed on the queue, ... When it's done it can reset the ManualResetEvent and wait until ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to improve performance of Queue accessing between 2 threads?
    ... fast as possible once Queue is not empty. ... AutoReset- or ManualResetEvent? ... Thats slow. ...
    (microsoft.public.dotnet.framework)
  • Re: Relationship between Application.Exit() and AppDomain
    ... it couldn't care less whether your app is WPF or WinForms ... message queue and returns immediately; ... You really should break on PostMessage and PostThreadMessage, because it's entirely possible that PostQuitMessage did exactly what the documentation described, but the .NET method triggered ending the message loop so GetMessage was never called. ... That's the portion that needs to coordinate with the DWM composition, that's the part that handles PQM when called from a WPF application running on Vista with DWM enabled. ...
    (microsoft.public.dotnet.framework.clr)