Thread: Updating ListBox during lengthy operation

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    Updating ListBox during lengthy operation

    Through WM_COMMAND i call a function that will process something and put out results to a listbox.

    Im trying to call

    Code:
      
    // inside the lengthy function
    while(PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
    {
            if (msg.message == WM_QUIT)
                break;
            TranslateMessage (&msg) ;
            DispatchMessage (&msg) ;
    }
    
        UpdateWindow(hWnd);
        RedrawWindow(hWnd, 0, 0,
                     RDW_INVALIDATE | RDW_ERASE | RDW_ERASENOW | RDW_UPDATENOW);
    to update the listbox but apparently its not enough or I dont call PeekMessage the right way.
    I dont know where should i put PeekMessage?
    Right now I use GetMessage also in WinMain.
    Last edited by Ducky; 01-13-2012 at 12:13 PM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try changing the RedrawWindow() to an InvalidateRect() followed by an UpdateWindow(). This combo generates a WM_PAINT msg but posts it directly to the HWNDs callback (bypassing the msg queue). Also remove the PeekMsg pump.

    I generally use a worker thread to do lengthy operations and use PostMessage() to send UI updates to the main thread. This ensures my UI remains responsive.
    If you want to get complicated register a HWND_BROADCAST msgs (or similar) to send the ListBox items to the main thread and have the main thread add them to the control.

    Part of your problem is that DispatchMessage() blocks until the message has been processed. As you are generating msgs (ie WM_ERASEBKGRND ) your PeekMsg will always be finding new msgs to process.

    Using Messages and Message Queues
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thanks Novocain!

    With a workerthread its working well.
    Also your other advices will serve me in the future.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hexadecimal bit operation
    By vlrk in forum C Programming
    Replies: 2
    Last Post: 10-17-2008, 10:04 AM
  2. Updating
    By ColdFire in forum Windows Programming
    Replies: 14
    Last Post: 03-27-2008, 08:29 AM
  3. new operation
    By NewGuy100 in forum C Programming
    Replies: 3
    Last Post: 07-28-2005, 10:45 AM
  4. listbox initiated sub listbox menus
    By Akhil in forum Windows Programming
    Replies: 0
    Last Post: 03-23-2005, 05:59 AM
  5. Illegal operation
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 03-07-2002, 04:33 PM