Solved

The proper way of using the PeekMessage function.

Posted on 1998-01-06
9
291 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:ketireddys120897
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 7

Expert Comment

by:faster
ID: 1313513
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.


0
 

Author Comment

by:ketireddys120897
ID: 1313514
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.
0
 

Author Comment

by:ketireddys120897
ID: 1313515
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.
0
 
LVL 7

Expert Comment

by:faster
ID: 1313516
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:chensu
ID: 1313517
"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".
0
 
LVL 3

Expert Comment

by:shaig
ID: 1313518
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.
0
 
LVL 3

Expert Comment

by:shaig
ID: 1313519
Oh... and don't open a new thread just for simple idle time proccessing!
0
 

Author Comment

by:ketireddys120897
ID: 1313520
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.
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 50 total points
ID: 1313521
MSG msg;

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 106
how do i create updater to My Activex application? 3 79
tripleUp challenge 7 74
Not needed 13 95
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now