Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. PostMessage question...

PostMessage question...

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomdebugginghelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rmnowick
    wrote on last edited by
    #1

    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

    A R 2 Replies Last reply
    0
    • R rmnowick

      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

      A Offline
      A Offline
      Antony M Kancidrowski
      wrote on last edited by
      #2

      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.

      R 1 Reply Last reply
      0
      • R rmnowick

        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

        R Offline
        R Offline
        Roger Allen
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Antony M Kancidrowski

          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.

          R Offline
          R Offline
          rmnowick
          wrote on last edited by
          #4

          Antony, Thanks for the tip. I switched the call to PostThreadMessage and in my thread I now periodically check for message arrival with PeekMessage and am seeing them there. Robert

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups