Question about messaging in C#
-
Greetings: What is the accepted method used for forms and/or background processes to communication with one another asynchronously under C#. For example, I have written my share of Windows apps under C++ and MFC that involve handling serial ports, USB ports or other such external sources. My usual approach is to monitor the port with a worker thread for incoming data. I might have a display window in the application that simply waits for incoming messages which can come in at any time without prompting. I usually create some sort of messaging structure and pass messages from the monitoring thread to the window using PostMessage. For those not familiar, PostMessage simply puts the message into the message queue and does NOT wait for a response or any indication that the message was received. This is in contrast to SendMessage, which waits for the message to be handled and is therefore not much different from calling the handler directly. So I am looking for a means to do something similar under C# and .NET. How is this accomplished in general? Thank you in advance to anyone who responds. Mark
-
Greetings: What is the accepted method used for forms and/or background processes to communication with one another asynchronously under C#. For example, I have written my share of Windows apps under C++ and MFC that involve handling serial ports, USB ports or other such external sources. My usual approach is to monitor the port with a worker thread for incoming data. I might have a display window in the application that simply waits for incoming messages which can come in at any time without prompting. I usually create some sort of messaging structure and pass messages from the monitoring thread to the window using PostMessage. For those not familiar, PostMessage simply puts the message into the message queue and does NOT wait for a response or any indication that the message was received. This is in contrast to SendMessage, which waits for the message to be handled and is therefore not much different from calling the handler directly. So I am looking for a means to do something similar under C# and .NET. How is this accomplished in general? Thank you in advance to anyone who responds. Mark
-
Greetings: What is the accepted method used for forms and/or background processes to communication with one another asynchronously under C#. For example, I have written my share of Windows apps under C++ and MFC that involve handling serial ports, USB ports or other such external sources. My usual approach is to monitor the port with a worker thread for incoming data. I might have a display window in the application that simply waits for incoming messages which can come in at any time without prompting. I usually create some sort of messaging structure and pass messages from the monitoring thread to the window using PostMessage. For those not familiar, PostMessage simply puts the message into the message queue and does NOT wait for a response or any indication that the message was received. This is in contrast to SendMessage, which waits for the message to be handled and is therefore not much different from calling the handler directly. So I am looking for a means to do something similar under C# and .NET. How is this accomplished in general? Thank you in advance to anyone who responds. Mark
Hello Well, you can do almost the same with C# using
SerialPort
class, but for lpt, USB, and other ports or devices you have to do it this way: 1- Call API functionCreateFile()
with the port/device name. 2- Get the handle returned by that call and pass it toFileStream
constructor 3- Use thatFileStream
object to read/write from/to the port/device. I hope that was close enough.Regards:rose:
-
Hello Well, you can do almost the same with C# using
SerialPort
class, but for lpt, USB, and other ports or devices you have to do it this way: 1- Call API functionCreateFile()
with the port/device name. 2- Get the handle returned by that call and pass it toFileStream
constructor 3- Use thatFileStream
object to read/write from/to the port/device. I hope that was close enough.Regards:rose:
Hi Nader, thanks for your response. I guess the example I gave was a bit too specific. In general, I am looking for the general accepted way for threads and forms to send messages to each other asynchronously. For a more general example, Lets say there is a parent form that launches another form using "Show" so that the new form behaves like a modeless dialog. Now, the new form has controls of various stripes on it and the parent form need not know about everything that is going on on this form but needs to be notified of certain key events. And lets say that simply calling some member function of the parent form from the child form isn't quite going to work. In Win32 programming and MFC, we would send asynchronous messaged from one window to another using the messaging system built into windows. As long as your thread or window has a message pump, then it should get the message. Is there anything like this for C#? Regards, Mark
-
Hi Nader, thanks for your response. I guess the example I gave was a bit too specific. In general, I am looking for the general accepted way for threads and forms to send messages to each other asynchronously. For a more general example, Lets say there is a parent form that launches another form using "Show" so that the new form behaves like a modeless dialog. Now, the new form has controls of various stripes on it and the parent form need not know about everything that is going on on this form but needs to be notified of certain key events. And lets say that simply calling some member function of the parent form from the child form isn't quite going to work. In Win32 programming and MFC, we would send asynchronous messaged from one window to another using the messaging system built into windows. As long as your thread or window has a message pump, then it should get the message. Is there anything like this for C#? Regards, Mark
Well, I think you are getting a little meticulous about details here:-D. It's nice though not much seen these days. Exchanging messages between forms has many forms;P, depending on your needs and design. There may not be a best practice that is considered an industrial standard or a windows logo criteria, rather it's just a mere advice that could be criticized by another programmer.
Jethro63 wrote:
Now, the new form has controls of various stripes on it and the parent form need not know about everything that is going on on this form but needs to be notified of certain key events
Once I read that sentence I had the word
interface
jumping across my eyes. It would be a nice way to make a standard of communicaion between your parent form and different MDIChild types -ie. through a common interface-.Jethro63 wrote:
And lets say that simply calling some member function of the parent form from the child form isn't quite going to work
Well, actually it can be done using
Parent.Invoke()
method for events that you want your child form to wait for the parent to excute its code. If you want to fire an event in the parent form and not to wait until it's done, callParent.BeginInvoke()
instead. Both calls of course are from your child forms.Jethro63 wrote:
In Win32 programming and MFC, we would send asynchronous messaged from one window to another using the messaging system built into windows. As long as your thread or window has a message pump, then it should get the message.
Well, you still can use the same thing in C# if you like it. Actually the messaging system is still applicable -even in Windows Vista I think-, so simply: 1- In the child form call Windows API
PostMessage()
orSendMessage()
functions -whichever suits you-, using P/Invoke and give it the handle of the parent form. 2- In the parent form implementIMessageFilter
interface and use itsPreFilterMessage()
to catch your custom message or use any way that suits you -usually there are predefined handlers- to catch WM_ standard messages if you wish to send them.Jethro63 wrote:
Is there anything like this for C#?
In addition to the above there are more improvised ways like exchanging data between forms -considered as thr
-
Well, I think you are getting a little meticulous about details here:-D. It's nice though not much seen these days. Exchanging messages between forms has many forms;P, depending on your needs and design. There may not be a best practice that is considered an industrial standard or a windows logo criteria, rather it's just a mere advice that could be criticized by another programmer.
Jethro63 wrote:
Now, the new form has controls of various stripes on it and the parent form need not know about everything that is going on on this form but needs to be notified of certain key events
Once I read that sentence I had the word
interface
jumping across my eyes. It would be a nice way to make a standard of communicaion between your parent form and different MDIChild types -ie. through a common interface-.Jethro63 wrote:
And lets say that simply calling some member function of the parent form from the child form isn't quite going to work
Well, actually it can be done using
Parent.Invoke()
method for events that you want your child form to wait for the parent to excute its code. If you want to fire an event in the parent form and not to wait until it's done, callParent.BeginInvoke()
instead. Both calls of course are from your child forms.Jethro63 wrote:
In Win32 programming and MFC, we would send asynchronous messaged from one window to another using the messaging system built into windows. As long as your thread or window has a message pump, then it should get the message.
Well, you still can use the same thing in C# if you like it. Actually the messaging system is still applicable -even in Windows Vista I think-, so simply: 1- In the child form call Windows API
PostMessage()
orSendMessage()
functions -whichever suits you-, using P/Invoke and give it the handle of the parent form. 2- In the parent form implementIMessageFilter
interface and use itsPreFilterMessage()
to catch your custom message or use any way that suits you -usually there are predefined handlers- to catch WM_ standard messages if you wish to send them.Jethro63 wrote:
Is there anything like this for C#?
In addition to the above there are more improvised ways like exchanging data between forms -considered as thr