PostMessage question...
-
All, I posted this message on the following board, but since this area gets more traffic, and also since I don't believe the problem is necessarily related to the serial code, I will repost it here: http://www.codeproject.com/system/serial.asp?target=serial Ramon, I'm guessing that you might be the only one that might be able to answer this. I have had great success incorporating your sample code into my project. I am controlling an external device that I write to once a second to start it performing a series of A/D conversions. When it is done it sends the data back, and the reader thread notifies my dialog box that there is data via the PostMessage routine. So far so good. Because I had a need to send data to the external device every second, I created a second writer thread, similar to the reader thread. I essentially copied the code and changed names and such and I now bring up both threads. My main dialog window is still the destination for the messages that get sent by the PostMessage routine. Here is my problem. The application that I incorporated your code into has a hard coded loop in the main DialogProc routine. This is perhaps not good programming practice as it hangs the code from processing any windows messages, including the messages from PostMessage that indicate that there is serial data to be read. What happens is then that I miss the serial data. What I need to be able to do is run that main loop AND process the messages that indicate there is serial data coming in. I tried to solve this by creating a new window within the writer thread that has it's own doalog processor for windows messages. Here is that code:
wc.style = CS\_HREDRAW | CS\_VREDRAW; wc.lpfnWndProc = (WNDPROC) MyWindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI\_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC\_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE\_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "MyWindowClass"; returned\_class\_atom = RegisterClass(&wc); if (!returned\_class\_atom) { last\_err = GetLastError(); } // Create window to handle the incoming serial data arrival notifications agilent\_receiver = CreateWindow("MyWindowClass", // Was "MyWindowClass" "Nothing", WS\_MAXIMIZE, CW\_USEDEFAULT, CW\_USEDEFAULT, CW\_USEDEFAULT, CW\_USEDEFAULT, HWND\_MESSAGE, NULL, NULL, lpvoid\_null); if (agilent\_receiver == NULL) { debug\_stop = 1; last\_err = GetLastError(); }
This code a
-
All, I posted this message on the following board, but since this area gets more traffic, and also since I don't believe the problem is necessarily related to the serial code, I will repost it here: http://www.codeproject.com/system/serial.asp?target=serial Ramon, I'm guessing that you might be the only one that might be able to answer this. I have had great success incorporating your sample code into my project. I am controlling an external device that I write to once a second to start it performing a series of A/D conversions. When it is done it sends the data back, and the reader thread notifies my dialog box that there is data via the PostMessage routine. So far so good. Because I had a need to send data to the external device every second, I created a second writer thread, similar to the reader thread. I essentially copied the code and changed names and such and I now bring up both threads. My main dialog window is still the destination for the messages that get sent by the PostMessage routine. Here is my problem. The application that I incorporated your code into has a hard coded loop in the main DialogProc routine. This is perhaps not good programming practice as it hangs the code from processing any windows messages, including the messages from PostMessage that indicate that there is serial data to be read. What happens is then that I miss the serial data. What I need to be able to do is run that main loop AND process the messages that indicate there is serial data coming in. I tried to solve this by creating a new window within the writer thread that has it's own doalog processor for windows messages. Here is that code:
wc.style = CS\_HREDRAW | CS\_VREDRAW; wc.lpfnWndProc = (WNDPROC) MyWindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI\_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC\_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE\_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "MyWindowClass"; returned\_class\_atom = RegisterClass(&wc); if (!returned\_class\_atom) { last\_err = GetLastError(); } // Create window to handle the incoming serial data arrival notifications agilent\_receiver = CreateWindow("MyWindowClass", // Was "MyWindowClass" "Nothing", WS\_MAXIMIZE, CW\_USEDEFAULT, CW\_USEDEFAULT, CW\_USEDEFAULT, CW\_USEDEFAULT, HWND\_MESSAGE, NULL, NULL, lpvoid\_null); if (agilent\_receiver == NULL) { debug\_stop = 1; last\_err = GetLastError(); }
This code a
I am not fully sure what you are trying to achieve but when posting messages between threads you really should use
PostThreadMessage()
PostThreadMessage posts messages to the queue of the specified thread without waiting.:) Ant. -
All, I posted this message on the following board, but since this area gets more traffic, and also since I don't believe the problem is necessarily related to the serial code, I will repost it here: http://www.codeproject.com/system/serial.asp?target=serial Ramon, I'm guessing that you might be the only one that might be able to answer this. I have had great success incorporating your sample code into my project. I am controlling an external device that I write to once a second to start it performing a series of A/D conversions. When it is done it sends the data back, and the reader thread notifies my dialog box that there is data via the PostMessage routine. So far so good. Because I had a need to send data to the external device every second, I created a second writer thread, similar to the reader thread. I essentially copied the code and changed names and such and I now bring up both threads. My main dialog window is still the destination for the messages that get sent by the PostMessage routine. Here is my problem. The application that I incorporated your code into has a hard coded loop in the main DialogProc routine. This is perhaps not good programming practice as it hangs the code from processing any windows messages, including the messages from PostMessage that indicate that there is serial data to be read. What happens is then that I miss the serial data. What I need to be able to do is run that main loop AND process the messages that indicate there is serial data coming in. I tried to solve this by creating a new window within the writer thread that has it's own doalog processor for windows messages. Here is that code:
wc.style = CS\_HREDRAW | CS\_VREDRAW; wc.lpfnWndProc = (WNDPROC) MyWindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI\_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC\_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE\_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "MyWindowClass"; returned\_class\_atom = RegisterClass(&wc); if (!returned\_class\_atom) { last\_err = GetLastError(); } // Create window to handle the incoming serial data arrival notifications agilent\_receiver = CreateWindow("MyWindowClass", // Was "MyWindowClass" "Nothing", WS\_MAXIMIZE, CW\_USEDEFAULT, CW\_USEDEFAULT, CW\_USEDEFAULT, CW\_USEDEFAULT, HWND\_MESSAGE, NULL, NULL, lpvoid\_null); if (agilent\_receiver == NULL) { debug\_stop = 1; last\_err = GetLastError(); }
This code a
If you have a hard coded lopp in your UI thread that stops it processing messages, you can add this code to the loop to allow other messages to be processed:
MSG msg; while (::PeekMessage(&msg, NULL, 0, 0, PM\_NOYIELD | PM\_REMOVE)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); }
Be careful as the cancel/ok buttons and the original button that set you loop going etc will be able to be used by the user. You need to set the dialog up in such cases that all commands you don;t want to be run during this long process are disabled. Roger Allen - Sonork 100.10016 Strong Sad: Clever I am? Next to no one. Undiscovered and soggy. Look up. Look down. They're around. Probably laughing. Still, bright, watery. Listed among the top. Ten. Nine. Late night. Early morn. Early mourn. Now I sleep.
-
I am not fully sure what you are trying to achieve but when posting messages between threads you really should use
PostThreadMessage()
PostThreadMessage posts messages to the queue of the specified thread without waiting.:) Ant.