Thread: trying to understand the message loop

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    188

    trying to understand the message loop

    hello, i just started a new job and it requires c++, and i'm confused trying to implement a simple program which watches a file for changes.

    i followed a tutorial and sample code online, and i'm trying to implement the asynchronous version using CreateFile and ReadDirectoryChangesW with a completion routine. the code seems to work fine, but only does depending on how i program the message loop.

    if i use the "standard" message loop:

    Code:
    while (GetMessage(&msg, NULL, 0, 0) > 0) {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
    the breakpoint i set in the completion function never hits. nothing seems to happen. however, if i use the following code:

    Code:
    while (true) {
      while (WAIT_IO_COMPLETION == MsgWaitForMultipleObjectsEx(0, NULL, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE))
        /* Do nothing */;
    
      while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
        if (msg.message == WM_QUIT) {
          return 0;
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
    }
    it works...

    what's the difference? thanks.

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Hi,

    did you already looked at this http://msdn.microsoft.com/en-us/libr...28(VS.85).aspx ?


    Greetz

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or perhaps you want to look at the MSDN MsgWaitForMultipleObjectsEx help page, where it explains what MWMO_ALERTABLE means?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Quote Originally Posted by MSDN
    Using a completion routine. To receive notification through a completion routine, do not associate the directory with a completion port. Specify a completion routine in lpCompletionRoutine. This routine is called whenever the operation has been completed or canceled while the thread is in an alertable wait state.
    Alertable Wait Functions

    gg

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    thanks for the replies...it makes a lot more sense now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Desperate Over Ecs!
    By cookie in forum C Programming
    Replies: 17
    Last Post: 07-16-2008, 01:25 PM
  2. Window message loop (Keys)
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2006, 05:15 PM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. Help, the window gets killed......
    By incognito in forum Game Programming
    Replies: 2
    Last Post: 05-28-2002, 02:22 PM

Tags for this Thread