473,698 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread Communication in C#

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 PostThreadMessa ge 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.
Jan 16 '06 #1
8 16570
Jayme,

Well, if you want a completely managed solution, I would use a
ManualResetEven t, 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 PostThreadMessa ge if
you wish, making the calls through the P/Invoke layer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jayme Pechan" <ja**********@w hitefeld.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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 PostThreadMessa ge 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.

Jan 16 '06 #2
If I use ManualResetEven t, 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]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:ez******** ******@tk2msftn gp13.phx.gbl...
Jayme,

Well, if you want a completely managed solution, I would use a
ManualResetEven t, 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 PostThreadMessa ge if
you wish, making the calls through the P/Invoke layer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jayme Pechan" <ja**********@w hitefeld.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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 PostThreadMessa ge 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.


Jan 16 '06 #3
Jayme,

If this is the case (you need to send data with the message), then just
use the GetMessage and PostThreadMessa ge functions. You can find the
declarations at http://www.pinvoke.net.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jayme Pechan" <ja**********@w hitefeld.com> wrote in message
news:ui******** ******@TK2MSFTN GP12.phx.gbl...
If I use ManualResetEven t, 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]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:ez******** ******@tk2msftn gp13.phx.gbl...
Jayme,

Well, if you want a completely managed solution, I would use a
ManualResetEven t, 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 PostThreadMessa ge
if you wish, making the calls through the P/Invoke layer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jayme Pechan" <ja**********@w hitefeld.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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 PostThreadMessa ge 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.



Jan 16 '06 #4
Jayme Pechan <ja**********@w hitefeld.com> wrote:
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 PostThreadMessa ge 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?


I'd suggest using some variation on a producer/consumer queue (which is
basically what the message queue is). See
http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml
(Half way down the page.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 16 '06 #5
What about inverting your control somewhat. You could have a class for
each type of task, and have that class responsible for dispatching work
on the threadpool. Each class could then implement locking to prevent
multiple threads from working on the same task at the same time?


Jayme Pechan wrote:
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 PostThreadMessa ge 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.

Jan 16 '06 #6
Jayme,
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 PostThreadMessa ge 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?


One way to do this is through the use of delegates. Just after the threads
are created pass a delegate for posting messages to the service and retrieve
a delegate to post a message to the thread, etc. Each thread of the service
would then be responsible for posting messages to the service and for
managing the messaging that are sent to it.

The other way I can imagine to acheive this is to implement custom commands
on the service. This has the advantage of exposing the messaging capability
through the service control manager. That could rather be a weakness
instead. It depends on your perspective.

Regards,

Randy
Jan 16 '06 #7
"Randy A. Ynchausti" <ra************ *@msn.com> wrote in message
news:up******** ******@TK2MSFTN GP12.phx.gbl...
One way to do this is through the use of delegates. Just after the
threads are created pass a delegate for posting messages to the service
and retrieve a delegate to post a message to the thread, etc. Each thread
of the service would then be responsible for posting messages to the
service and for managing the messaging that are sent to it.


I like this. Even further, I was privately wondering to myself, if these
are one-way messages why not use .NET events?

-- Alan
Jan 16 '06 #8

"Randy A. Ynchausti" <ra************ *@msn.com> wrote in message
news:up******** ******@TK2MSFTN GP12.phx.gbl...
| Jayme,
|
| > 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 PostThreadMessa ge 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?
|
| One way to do this is through the use of delegates. Just after the
threads
| are created pass a delegate for posting messages to the service and
retrieve
| a delegate to post a message to the thread, etc. Each thread of the
service
| would then be responsible for posting messages to the service and for
| managing the messaging that are sent to it.
|
| The other way I can imagine to acheive this is to implement custom
commands
| on the service. This has the advantage of exposing the messaging
capability
| through the service control manager. That could rather be a weakness
| instead. It depends on your perspective.
|
| Regards,
|
| Randy
|
|

The OP is asking about in-process cross-thread message passing, not
cross-process.
The best solution for this is a producer/consumer queue as Jon proposed,

Willy.
Jan 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2215
by: David Sworder | last post by:
This message was already cross-posted to C# and ADO.NET, but I forgot to post to this "general" group... sorry about that. It just occured to me after my first post that the "general" group readers might have some thoughts on this perplexing .NET blocking issue. (see below) ===== Hi,
1
1646
by: Ayende Rahien | last post by:
Is it possible to use events as a communication mechanism between threads?
7
9441
by: Brett Robichaud | last post by:
I'm trying to decide on the right approach for communication between the UI and a worker thread in a WinForms app. I am very familiar with threads in the unmanaged C++ world and in the past have used WM_USER based messages to communicate status from the worker thread back to the UI thread. What is the right way to do this in .Net? Are asynchronous delegates the way to go, or is there a better (or just different) approach I should...
9
3071
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException System.Threading.ThreadInterruptedException System.Threading.ThreadStateException to see if I could get more information about why the thread stops running but that code is never executed. Any ideas on how I can debug this?
20
2406
by: Bob Day | last post by:
Using VS 2003, VB, MSDE... There are two threads, A & B, that continously run and are started by Sub Main. They instantiationsl of identical code. Thread A handles call activity on telephone line 1 and Thread B handles call activity on telephone line 2. They use a common SQL datasource, but all DataSets are unique to each thread. Is there a way for thread A to occasionally communication to thread B that something has happened? ...
1
2523
by: Fred B | last post by:
I am launching a new thread from my application's main process (using VB.net 2003), and I can't get the child to receive the parameter I'm attempting to send it in a named data slot. The code for launching the thread: Dim NewThread As New Thread(AddressOf LaunchCommThread) NewThread.AllocateNamedDataSlot("Offset") NewThread.IsBackground = True NewThread.Name = SIMclass.SIM(1).strCtrlDesignator
2
2321
by: Max | last post by:
Hello, I made an application that uses the main thread for the UI, and another thread to communicate through the RS232 port. I would like the communication thread to be suspended immediately when the user presses the ESC key. It should resume when the user answers a dialog box. The Thread.Suspend cannot be used anymore in .NET and I'm trying to figure out how to do this differently. Can anyone help? Cheers, Max.
6
5121
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be performed on the same thread thread that instantiated the class. One way to do this is to pass an ISynchronizeInvoke into the class and use it to synchronize the callback. In the constructor of the class, could I take note of the current thread...
1
2050
by: NagarajanS | last post by:
hi, i have a problem in cross thread communication.my problem is i drog some controls in the form.now i created new thread inside the coding of form.cs.when i try to run that thread it shows the "cross thread communication".is any possible to send the user control objects to the new thread?or what i have to eliminate this error. regards nags
12
2077
by: Ronny | last post by:
Thanks Chris, Looks nice but I miss the dual way communication. In the main thread to deliver paramters and data to the worker thread- how can I do that? Regards Ronny Take a look at the background worker thread component. It has what you want built into it. http://msdn.microsoft.com/en-us/library/8xs8549b.aspx
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.