Solved

Equivalent in C to VB's DoEvents?

Posted on 1998-12-31
5
400 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.

0
Comment
Question by:DerechaEn
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1255518
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
 
LVL 5

Expert Comment

by:heathprovost
ID: 1255519
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
 
LVL 11

Expert Comment

by:alexo
ID: 1255520
There is no nees for Sleep() calls.  Win32 uses preemptive multitasking.
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1255521
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
 
LVL 11

Expert Comment

by:alexo
ID: 1255522
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

708 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

14 Experts available now in Live!

Get 1:1 Help Now