We help IT Professionals succeed at work.

Equivalent in C to VB's DoEvents?

DerechaEn
DerechaEn asked
on
Medium Priority
511 Views
Last Modified: 2010-04-15
I'm a former VB programmer who's moved on to C for sometime know, but as I've been learning how to do things in C that I'd done in Visual Basic, I keep coming across the need for an equivalent to the results of VB's DoEvents function.

I'm working on a program that runs in the background and waits for a program to load. Lets say I'm waiting for Notepad. In VB, I'd make a FindWindow loop, like this:

Dim hwndNotepad As Integer

Do
      DoEvents
      hwndNotepad = FindWindow("Notepad", vbNullString)
Loop Until (hwndNotepad <> 0)

Now without that DoEvents line, my program won't be able to accept input while the loop is going. The window won't be able to be resized, no button on it can be clicked, it can't even be closed. DoEvents fixes that. But in C, I'm writing this:

HWND hwndNotepad;

do
{
      hwndNotepad = FindWindow("Notepad", NULL);
} while (hwndNotepad == NULL);

That loop will of course lock up the program until Notepad comes along and ends it. But I want to be able to do other things with the program, even quit it, while the loop is going.

Is there an API function that can simply magically take care of this? Or will this require a large complicated restructuring of things? I refuse to believe that a Visual Basic function can't be matched in C.

Thanks,

Chris B.

Comment
Watch Question

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
I would recommed inserting sleep() followed by PeekMessage() wherever you would normally put doevents.  The doevents function in VB does almost exactly the same thing.  Sleep(0) will relinquish the remainder of the threads timeslice to any other threads that are ready to run.  If no other threads need CPU then your loop will continue. PeekMessage() will check the message que for your message loop.

Commented:
There is no nees for Sleep() calls.  Win32 uses preemptive multitasking.
Alexo, that is the very reason you use a sleep call.  When called with a parameter of "0", this function relinquishes the remainder of the threads time slice back to the OS if another thread is waiting for processor time.  In a lengthy repetive function, it can make the difference between a smooth user interface and a completely unresponsive one.

Commented:
heathprovost, I beg to differ.  If you provide for message handling during the processing, there will be no need for "manual" context switches.  However if you have a reference that proves me wrong please provide it.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.