The proper way of using the PeekMessage function.

I'm developing a program which includes some background processing. If i want to stop the process through the stop button i've provided for the user, how can i do it. i tried to use the PeekMessage function. But something seems to be missing. Could any one show me the right way? Thank you.
ketireddys120897Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fasterCommented:
If you are using windows 95 or NT, the better way is to create another thread for the background processing, the main thread deal with UI and terminate the worker thread when user cancel the operation.

PeekMessage() is used in 16 bit windows to solve this kind of problem, but it is not a good solution.


ketireddys120897Author Commented:
Yeah. Sure that is one way of solving it. But what if i want only one thread running? It should be possible to peek into the message queue in between and process the message.
ketireddys120897Author Commented:
Yeah. Sure that is one way of solving it. But what if i want only one thread running? It should be possible to peek into the message queue in between and process the message.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

fasterCommented:
What problem do you have now and how do you write your code?

Although PeekMessage() can solve your problem, it is not the recommended way in win32.  Creating another thread will be much simpler.
chensuCommented:
"Many applications perform lengthy processing "in the background." Sometimes performance considerations dictate using multithreading for such work. Threads involve extra development overhead, so they are not recommended for simple tasks like the idle-time work that MFC does in the OnIdle function."

If you are using Visual C++ 5.0, you may find the documentation about how to use PeekMessage loop to perform a background task.

Visual C++/Visual C++ Programmer's Guide/Adding Program Functionality/Details/MFC Topics (General)/Idle Loop Processing

For Visual C++ 4.x, you may search for the keywords "PeekMessage" and "OnIdle".
shaigCommented:
If you are using MFC:
Your application class ( inherits fro CWinApp ) has an virtual OnIdle function.  override it for idle time proccesing.
Use the ON_MESSAGE macros for catching button messages.
What you described is trivial in MFC, if you have any problems email me.
shaigCommented:
Oh... and don't open a new thread just for simple idle time proccessing!
ketireddys120897Author Commented:
Thank you very much for the response. I solved the problem in an other way. But it would be great if someone can give me the a few lines of code implementing the PeekMessage function.
Tommy HuiEngineerCommented:
MSG msg;

while (PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE)
{
  if (GetMessage(&msg))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.