Synchronizing a function with the message loop?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Synchronizing a function with the message loop?

  1. #1
    Join Date
    May 2010
    Location
    .Net 4.0
    Posts
    58

    Synchronizing a function with the message loop?

    Hello,

    I'm a brand new WinAPI programmer trying to figure out the language, and I'm a bit stuck. I have the following scenario:

    I am controlling a laboratory instrument via the instrument's operating software, which responds to Windows messages and sends back its own. I am therefore able to give a SendMessage instruction at any time and from any function, and then listen for the response via the message loop and WndProc (it sends back a WM_COPYDATA). I have tested my code and am able to both send and receive messages successfully. However, I'm trying to program the following function and can't figure out how:

    (pseudocode):
    x = 0
    while x < 10
    {
    Send command #1 (set temperature to x).
    Send command #2 (send me your status (via the message loop)).
    while (status != ready and time elapsed < 1 minute) { wait }
    x++
    }

    I can't figure out how to allow my function to receive the message from the message handler in real time. Is this even possible? If not, what is another approach I could use?

  2. #2
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Synchronizing a function with the message loop?

    I think you could use the same approach video games use - they basically let the message loop run as fast as possible, getting (and making) status updates on each iteration. So your program is just one big loop that runs over and over with the following structure: (1) get current state/messages, (2) do calculations and make changes to the state if required, else do nothing, (3) repeat. So, the main thing is not to do something that will block the message loop when it's not supposed to be blocked; that is, don't call a function that will take too long to complete or block because it awaits user input, if that is not appropriate/acceptable for the given moment in time. Don't write code that will make your program "wait" - rather, if the program should wait (do nothing) and if the wait time has not elapsed, simply do nothing for that iteration. Don't create additional loops if you don't have to, use the message loop itself (because it is, literally, a loop).
    Also, if a function that needs to do some work that will take significant amount of time needs to be called, but the program still needs to stay responsive, spawn a new thread and do that work there, without blocking the message loop tread (but be careful, there could be potential synchronization issues you will need to resolve). Anyway, this is how windows applications themselves work - the message loop runs and runs, and programmers usually offload any time consuming tasks to a different thread, to keep the UI responsive.

    Now, since this is about controlling a laboratory instrument, if there are potential hazards involved, notice that this interaction does not happen exactly in real time, but very closely in real time, assuming that the message loop is able to run many times per second, so make sure to take this into account.
    Last edited by TheGreatCthulhu; October 14th, 2013 at 07:25 PM.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,891

    Re: Synchronizing a function with the message loop?

    Also, note that some messages require the messageloop to be attached to an actual visible window.
    WM_COPYDATA is -afaik- among those.
    There's a sample on MSDN that shows how WM_COPYDATA works.

  4. #4
    Join Date
    May 2010
    Location
    .Net 4.0
    Posts
    58

    Re: Synchronizing a function with the message loop?

    Thanks guys! I managed to spawn my temperature controlling function in a new thread and have its function depend on a global temperature variable set by the message loop in the original thread. Thanks for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center