[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

Equivalent in C to VB's DoEvents?

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.

0
DerechaEn
Asked:
DerechaEn
  • 3
  • 2
1 Solution
 
alexoCommented:
This is not a C question and you should have asked it in the windows programming area.

There are several solutions to this problem:

1. Do lenghty processing in a secondary thread so the main thread will be free to handle incoming messasges.

2. Create a secondary message loop in another thread.  Take care to call AttachThreadInput() so that message dispatching will work correctly.

3. Do PeekMessage() processing in your lenghty operations.
    See for example: http://support.microsoft.com/support/kb/articles/q74/0/42.asp


0
 
heathprovostCommented:
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.
0
 
alexoCommented:
There is no nees for Sleep() calls.  Win32 uses preemptive multitasking.
0
 
heathprovostCommented:
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.
0
 
alexoCommented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now