Thread: DoEvents again...

  1. #1

    DoEvents again...

    I'm sure this is halfway to a faq but here goes.
    I need to pump the message loop in some processing, something like the vb doevents function.
    The usual implementation of this is something like:
    while ( PeekMessage(&Msg,NULL,0,0,PM_REMOVE) ) {
    The problem with this is that the window can be destroyed halfway through your processing. The expected solution to this would be not to process WM_DESTROY and WM_CLOSE:
    if (Msg.message == WM_CLOSE || Msg.message == WM_DESTROY)
    However, this does not work as WM_DESTROY and WM_CLOSE are not queued. ie. They are sent directly to the window procedure.

    The only way I can think of is to trap the close and destroy messages, either in the main window proc or one set up for the purpose and set a flag to repost them at the end of the processing. However, this seems klunky.

    Does anybody have a way of implementing doEvents without the window potentially disapearing?

  2. #2
    Sorry, I didn't mention that I'm using C, so no MFC, etc is available.

    Thanks for your time.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Usually if a user is trying to close your window, they want it gone. You can always catch WM_CLOSE in your callback and ask the user if they really want to exit. Also, under the WM_CLOSE message you could just put in some basic cleanup code to wrap up whatever you're doing and/or prompt to save the current data (maybe you could use sessions?), then make your call to DestroyWindow when you're all done cleaning up...

  4. #4
    Thanks alot for your reply.
    I should have checked this first. I thought the vb doevents function prevented the window being destroyed and I could emulate it. But on testing I found it also allows the window to be destroyed.
    Private Sub UserForm_Click()
    For x = 1 To 100000
        'code fails on next line after test msg has shown after all.
        Debug.Print Me.Caption 
    End Sub
    Private Sub UserForm_Terminate()
    MsgBox "test"
    End Sub
    So I'll just have to go with your solution, or put an IsWindow call after the call to my message pump to check whether the processing should be continued.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Statement similar to VB's "DoEvents"
    By XenoCodex Admin in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 06:29 AM