CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: PeekMessage

  1. #1
    Join Date
    Jun 2010
    Posts
    136

    PeekMessage

    Hi,
    I am trying to kill all my timers. So I am trying to put PeekMessage at the end to kill all wm_timer which left on the message queue. So that's my code is

    KillTimer(Timer_A);
    KillTimer(Timer_B);
    MSG m;
    while(PeekMessage(&m,NULL,WM_TIMER,WM_TIMER,PM_REMOVE))
    {
    KillTimer(m.wParam);
    Sleep(1);
    }

    The above code works on my work computer but it is not working on some other computers. I used remote debugger and tried to debug it. I am receiving 0x0113 for the m.wParam value which is a value of WM_TIMER. The problem is that if I remove the PeekMessage loop It cause some other problems. Could you please let me know what might be causing this issue?

    Regards,
    ABM

  2. #2
    Join Date
    Jun 2010
    Posts
    136

    Re: PeekMessage

    I am sorry I forgot to mention what's the problem is. PeekMessage loop always returning true and it becomes kind of infinite loop...

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: PeekMessage

    Why do you see the need to "remove" WM_TIMER messages. WM_TIMER messages are low-priority messages that are not even added to the queue unless both of the following conditions are met: (1) there are no other higher-priority messages in the queue, and (2) your program actually makes a call to GetMessage or PeekMessage. Per the documentation:
    The WM_TIMER message is a low-priority message. The GetMessage and PeekMessage functions post this message only when no other higher-priority messages are in the thread's message queue.
    Quote Originally Posted by ABM
    I am receiving 0x0113 for the m.wParam value which is a value of WM_TIMER.
    0x0113 is the value for WM_TIMER, but NOT the value of msg.wparam. The value of msg.wparam is the ID number for the timer. What value are you getting for msg.wparam, and is it the same as the ID of some timer in your program?

    Mike

  4. #4
    Join Date
    Jun 2010
    Posts
    136

    Re: PeekMessage

    Hi,
    This is the code written by someone. But it is working all the time. Actually, i have other projects for this is running fine. Also, this code is running fine on my computer but have problems with other computers.

    I am getting 0x00113 for msg.wParam which is strange to me too. I just checked i don't have internal timer which value is 0x00113. I don't know why I am receiving this strange behavior on some computers...

    Regards,
    ABM

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: PeekMessage

    KillTimer by itself should be enough.

    What's the point of the sleep?

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: PeekMessage

    Agreement with GCDEF. Based on the low priority of WM_TIMER, and the fact that it's posted only when Peek/GetMessage is called, the call to KillTimer should be enough to ensure that there are no timer messages in the queue.

    And what is the point of the call to Sleep?

    Quote Originally Posted by ABM
    The problem is that if I remove the PeekMessage loop It cause some other problems
    What other problems? And is your program multi-threaded?

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: PeekMessage

    What other problems?
    I can suspect some with PeekMessage. The loop pumps for only a special type of messages. And therefore, blocks other ones if any.

    The code is absolutely unnecessary as well as potentially dangerous. If I would want to do something like this (kill each and every timer but not only mine, which itself seems pretty weird) I were setting some global flag on which I killed all the timers in WM_TIMER handler.
    Last edited by Igor Vartanov; June 14th, 2012 at 01:02 AM.
    Best regards,
    Igor

  8. #8
    Join Date
    Jun 2010
    Posts
    136

    Re: PeekMessage

    Hi,
    Thanks for the replies. As I wrote before this is code written by someone else and I am using it. It was working for over 2 years since my application has released. We upgraded our software and started to see this result. This is a multithreaded application where we get live streaming video out of our manufactured cameras. The other problem is that if I remove the PeekMessage loop, cameras doesn't initialize properly next time. Camera is connected by USB so to make camera work next time i have to unplug the camera. We have another software which basically does the same thing, we are using the same code in that software too. This code runs fine in that software. I can take the Sleep out but I believe that this glitch might be causing because of some other code. But i don't know the reason for this to happen so I can't troubleshoot my code. You all are more experience than me. If you think PeekMessage is unnecessary than I can remove it. Please help...

    Regards,
    ABM

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: PeekMessage

    Okay, so it's a sort of cleanup procedure on exit. Fine, let it be that. Then we return to your initial request:
    The above code works on my work computer but it is not working on some other computers.
    Please define not working.
    Best regards,
    Igor

  10. #10
    Join Date
    Jun 2010
    Posts
    136

    Re: PeekMessage

    Quote Originally Posted by Igor Vartanov View Post
    Okay, so it's a sort of cleanup procedure on exit. Fine, let it be that. Then we return to your initial request:
    Please define not working.
    Igor,
    I am sorry I am not good in writing these forums. What is not working is the PeekMessage loop. It is my code which works fine on my work computer but not on my release version of the program.


    void NukeAllTimers()
    {
    KillTimer(Timer_A);
    KillTimer(Timer_B);
    MSG m;
    while(PeekMessage(&m,NULL,WM_TIMER,WM_TIMER,PM_REMOVE))
    {
    KillTimer(m.wParam);
    Sleep(1);
    }
    }

    The problem is that the computers I am having this problem that the while loop are returning always true. So my programs halts and customer can't do anything else. We made lot of changes in our software and but we didnt' touch this part. This code is been running on our other software's and actually other releases of the software which is having this issue. So my guess would be that I must have done something on other part of the program which might be causing that. But unfortunately I am not experience enough what are the reason which might cause this to happen. So Basically kind of in the dark so can't troubleshoot the problem. Also, just to let you know it works sometimes too.
    Just let me know if you need some other information please let me know or If I didn't explain anything clearly..

    Regards,
    ABM
    }

  11. #11
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: PeekMessage

    What is the content of the MSG that's returned by PeekMessage? Give us all six fields, and let us know whether the value of the HWND makes sense in the context of the windows owned by the calling thread.

    In addition, it seems that you're in an infinite loop, returning many MSGs, so give us a sampling of them if the content differs.

    This is related to the initial question of why msg.wparam has a value of 0x0113, which doesn't make any sense at all.

    Incidentally, is it possible that your program is posting user-defined messages that just by coincidence have the same value as WM_TIMER (ie, 0x0113)?

    Mike

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: PeekMessage

    The problem is that the computers I am having this problem that the while loop are returning always true.
    Let's remember that this weird trick with forcible killing timers was introduced to "fix" your problem with USB device. From the very beginning you were supposed to fix original problem instead of pegging it up. Now you find that your peg fails to work sometimes or someplace. I would say it's time to get to your root problem and fix it.
    But unfortunately I am not experience enough what are the reason which might cause this to happen.
    Me neither.
    Best regards,
    Igor

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: PeekMessage

    Quote Originally Posted by ABM View Post
    But i don't know the reason for this to happen so I can't troubleshoot my code.
    Do you think that when we debug code, we always know what the reason is for the failure before we start troubleshooting? You have to start somewhere, and the place to start is to review your architecture and your code.
    You all are more experience than me. If you think PeekMessage is unnecessary than I can remove it. Please help...
    Looks like you need to hire a consultant to troubleshoot your issue if you can't do it yourself.

    More than likely, you've just been lucky using very dubious synchronization techniques such as Sleep() to bypass issues. I can tell you that using Sleep() to fix synchronization problems is not a fix. At some point, that call to Sleep() will come back to bite you, and unfortunate for you, it did just that.

    Regards,

    Paul McKenzie

  14. #14
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: PeekMessage

    Paul might be blunt but he's right. And candidly, I had already come to the same conclusion; my questions to you (ABM) were intended to lead you in the direction of realizing for yourself that there are some basic architectural errors in the code, probably long pre-dating your update.

    To me, the clues are obvious: You have a multi-threaded program that works on some machines but not others, and even on the machines it works on, the only way to get it to work is by a PeekMessage loop with a call to Sleep. It's obvious that you have synchronization issues and/or race conditions.

  15. #15
    Join Date
    Jun 2010
    Posts
    136

    Re: PeekMessage

    Quote Originally Posted by Paul McKenzie View Post
    Looks like you need to hire a consultant to troubleshoot your issue if you can't do it yourself.
    Paul,
    I fixed it. I was just trying to solve quickly that's why I was posted on this forum. I always knew that I would able to solve the problem but didn't want to spend too much time as my project deadlines were pressing. I don't know what makes you say that since I didn't ask anyone that do I need to hire a consultant to do that job.

    Regards,
    ABM

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured