Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6635829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:06:21+00:00 2026-05-25T23:06:21+00:00

i have a program which is using several threads to execute some task. Each

  • 0

i have a program which is using several threads to execute some task. Each thread is having a bunch of task to execute. after executing one of them, each thred will call a post message to main screen to update logs.

Now i have sixty thousand tasks ten thousand for each thread – six threads- after executing each task thread is calling post messages. but because of these post messages my application becomes very busy and looks like it hanged.

if i remove post messages…every thing works fine. But i cannot call the procedure directly because it uses ui controls and ui controls are not thread safe and calling procedure directly from thread will lead to other errors.

so is there any alternative available for postmessage and send message.

Thanks,
bASIL

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:06:21+00:00Added an answer on May 25, 2026 at 11:06 pm

    The problem is that there are two message queues for posted messages. The result of this is that your posted messages are always processed before any Paint, Input, or Timer messages.

    This means that you are flooding the message queue with a few hundred thousand messages. These messages will always get processed before paint and user messages – making your application appear to hang.

    The common way to solve this is to use a timer; have your code start a very short duration (e.g. 0 ms) timer.

    Clarification

    Timer messages (WM_TIMER), like Paint messages (WM_PAINT) and Input messages (e.g. WM_MOUSEMOVE, WM_KEYDOWN) are processed after posted messages. Timer messages specifically are processed after input and paint messages.

    This means that your application will respond to user events and paint requests before it handles the next WM_TIMER message. You can harness this behavior by using a Timer (and it’s WM_TIMER message), rather than posting a message yourself (which would always take priority over paint and input messages).

    When timers fire they send a WM_TIMER message. This message will always be handled after any input and paint messages; making your application appear responsive.

    The other upside of setting a timer is that it forces you to “queue up” all the items you have processed in a (thread safe) list. You combine a few thousand work items into one “timer”, saving the system from having to generate and process thousands of needless messages.

    Bonus Chatter

    …messages are processed in the following order:

    • Sent messages
    • Posted messages
    • Input (hardware) messages and system internal events
    • Sent messages (again)
    • WM_PAINT messages
    • WM_TIMER messages

    Update: You should not have a timer polling, that’s just wasteful and wrong. Your threads should “set a flag” (i.e. the timer). This allows your main thread to actually go idle, only waking when there is something to do.

    Update Two:

    Pseudo-code that demonstrates that order of message generation is not preserved:

    //Code is public domain. No attribution required.
    const
      WM_ProcessNextItem = WM_APP+3;
    
    procedure WindowProc(var Message: TMessage)
    begin
       case Message.Msg of
       WM_Paint: PaintControl(g_dc);
       WM_ProcessNextItem:
          begin
              ProcessNextItem();
              Self.Invalidate; //Invalidate ourselves to trigger a wm_paint
    
              //Post a message to ourselves so that we process the next 
              //item after any paints and mouse/keyboard/close/quit messages
              //have been handled
              PostMessage(g_dc, WM_ProcessNextItem, 0, 0); 
          end;
       else
          DefWindowProc(g_dc, Message.Msg, Message.wParam, Message.lParam);
       end;
    end;
    

    Even though i generate a WM_ProcessNextItem after i generate a WM_PAINT message (i.e. Invalidate), the WM_PAINT will never get processed, because there is always another posted message before it. And as MSDN says, paint messages will only appear if there are no other posted messages.

    Update Three: Yes there is only message queue, but here’s why we don’t care:

    Sent and posted messages

    The terminology I will use here is nonstandard, but I’m using it because I think it’s a little clearer than the standard terminology. For the purpose of this discussion, I’m going to say that the messages associated with a thread fall into three buckets rather than the more standard two:

    What I'll call them            Standard terminology
    ===========================    =============================
    Incoming sent messages         Non-queued messages
    Posted messages            \_  
    Input messages             /   Queued messages
    

    In reality, the message breakdown is more complicated than this, but we’ll stick to the above model for now, because it’s “true enough.”

    The Old New Thing, Practical Development Throughout the Evolution of Windows
    by Raymond Chen
    ISBN 0-321-44030-7
    Copyright © 2007 Pearson Education, Inc.
    Chapter 15 – How Window Messages Are Delivered and Retrieved, Page 358

    It’s easier to imagine there are two message queues. No messages in the “second” one will be read until the “first” queue is empty; and the OP is never letting the first queue drain. As a result none of the “paint” and “input” messages in the second queue are getting processed, making the application appear to hang.

    This is a simplification of what actually goes on, but it’s close enough for the purposes of this discussion.

    Update Four

    The problem isn’t necessarily that you are “flooding” the input queue with your messages. Your application can be unresponsive with only one message. As long as you have one posted message in the queue, it will be processed before any other message.

    Imagine a series of events happen:

    • Mouse is moved (WM_MOUSEMOVE)
    • Mouse left button is pushed down (WM_LBUTTONDOWN)
    • Mouse left button is released (WM_LBUTTONUP)
    • The user moves another window out of the way, causing your app to need to repaint (WM_PAINT)
    • Your thread has an item ready, and Posts a notification (WM_ProcessNextItem)

    Your application’s main message loop (which calls GetMessage) will not receive messages in the order in which they happened. It will retrieve the WM_ProcessNextItem message. This removes the message from the queue, leaving:

    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_PAINT

    While you process your item the user moves the mouse some more, and clicks randomly:

    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_PAINT
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP

    In response to your WM_ProcessNextItem you post another message back to yourself. You do this because you want to process the outstanding messages first, before continuing to process more items. This will add another posted message to the queue:

    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_PAINT
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_ProcessNextItem

    The problem starts to become apparent. The next call to GetMessage will retrieve WM_ProcessNextItem, leaving the application with a backlog of paint and input messages:

    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_PAINT
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP
    • WM_MOUSEMOVE
    • WM_MOUSEMOVE
    • WM_LBUTTONDOWN
    • WM_LBUTTONUP

    The solution is to take advantage of the out-of-order processing of messages. Posted messages are always processed before Paint/Input/Timer messages: don’t use a posted message. You can think of the message queue as being divided into two groups: Posted messages and Input messages. Rather than causing the situation where the “Posted” messages queue is never allowed to empty:

    Posted messages      Input messages
    ==================   =====================
    WM_ProcessNextItem   WM_MOUSEMOVE
                         WM_LBUTTONDOWN
                         WM_LBUTTONUP
                         WM_PAINT
    

    You an use a WM_TIMER message:

    Posted messages      Input messages
    ==================   =====================
                         WM_MOUSEMOVE
                         WM_LBUTTONDOWN
                         WM_LBUTTONUP
                         WM_PAINT
                         WM_TIMER
    

    Nitpickers Corner: This description of two queues isn’t strictly true, but it’s true enough. How Windows delivers messages in the documented order is an internal implementation detail, subject to change at any time.

    It’s important to note that you’re not polling with a timer, that would be bad™. Instead you’re firing a one-shot timer as a mechanism to generate a WM_TIMER message. You’re doing this because you know that timer messages will not take priority over paint or input messages.

    Using a timer has another usability advantage. Between WM_PAINT, WM_TIMER, and input messages, there is out-of-order processing to them as well:

    • input messages, then
    • WM_PAINT, then
    • WM_TIMER

    If you use a timer to notify your main thread, you can also be guaranteed that you will process paint and user input sooner. This ensures that your application remains responsive. It’s a usability enhancement and you get it for free.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have a computation task which is split in several individual program executions, with
I have a C++ program using OpenMP, which will run on several machines that
I have a program which reads an XML file using the DOM functions: $doc
I have a program which has some Textareas / Labels these can be anywhere
Hello I have this program which reverses letters I enter. I'm using iostream .
I have a multi-threaded program, where I have one thread to watch over several
I am using several threads to do some heavy (and error-prone) processing on a
I have a C program which calls several functions from different files, and in
I have these several images which I need to be combined using BufferedImage in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.