Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2007

Tip: Looking for answers? Try searching our database.

pause the execution of code in one routine while letting the app respond to keyboard

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frank - 15 Jan 2007 20:24 GMT
I'm using  Sleep(100) and that works OK.

However, it would be better if my app continued to receive and process
keyboard messages.

How can I pause the execution of code in one routine while letting the app
respond to keyboard input?

Thanks
William DePalo [MVP VC++] - 15 Jan 2007 20:33 GMT
> I'm using  Sleep(100) and that works OK.
>
[quoted text clipped - 3 lines]
> How can I pause the execution of code in one routine while letting the app
> respond to keyboard input?

Is it in a managed or native application that you need to do that?

Regards,
Will
Frank - 15 Jan 2007 20:40 GMT
Native

>> I'm using  Sleep(100) and that works OK.
>>
[quoted text clipped - 8 lines]
> Regards,
> Will
William DePalo [MVP VC++] - 15 Jan 2007 21:23 GMT
> Native

Then you can try something along these lines:

   MSG msg;

   SetTimer(...);    // Set a timer to wake you up

  while ( GetMessage(&msg, 0, 0, 0) )
  {
    // If you have multiple timers make sure it
    //  is the one that wakes you up

    if ( msg.message == WM_TIMER )
    {
      KillTimer(...);
      break;
     }

    TranslateMessage(...);
    DispatchMessage(...);
  }

Regards,
Will
Frank - 15 Jan 2007 21:44 GMT
I have a loop like this in WinMain (without the timer).
Are you saying to modify that?

Or are you saying I can create a routine using your code that I can call,
instead of calling sleep, from some place in my program other than WinMain?

What I need is essentially the VB Application.DoEvents capability

Thanks for your patience

>> Native
>
[quoted text clipped - 21 lines]
> Regards,
> Will
William DePalo [MVP VC++] - 15 Jan 2007 23:00 GMT
>I have a loop like this in WinMain (without the timer).

Right.

> Are you saying to modify that?

No.

> Or are you saying I can create a routine using your code that I can call,
> instead of calling sleep, from some place in my program other than
> WinMain?

Yes.

> What I need is essentially the VB Application.DoEvents capability

Yes. If you need to do some stuff A, stall and then do some stuff B you put
a message loop between the A and B. This can get confusing so often, but not
always, UI elements that you don't want the user to click on are disabled
before the loop and reenabled afterwards. This is what the dialog manager in
Windows does when it displays a _modal_ dialog box.

In the loop I sketched, I set a timer to ensure that the loop would see a
WM_TIMER message which you could take as a queue to end the loop.

> Thanks for your patience

You are welcome.

Regards,
Will
Frank - 16 Jan 2007 00:31 GMT
Took 10 minutes, I had to look up some things but basically dropped your
code in and it worked great.

Thanks

>>I have a loop like this in WinMain (without the timer).
>
[quoted text clipped - 27 lines]
> Regards,
> Will
A Wieser - 16 Jan 2007 09:22 GMT
| > Native
|
[quoted text clipped - 21 lines]
| Regards,
| Will

This seems like a dangerous way to do things.  Your program becomes
reentrant at this point, so depending on what else is happening all sorts of
things can go wrong.

If you want to pause execution, why don't you break your routine into two
pieces, and set a timer for the amount of time you want to pause.
Then in your timer handler, post a message back to your window to schedule
the second half of the task.

Anthony Wieser
Wieser Software Ltd
William DePalo [MVP VC++] - 16 Jan 2007 17:21 GMT
> This seems like a dangerous way to do things.

It's not dangerous per se.

> Your program becomes reentrant at this point, so
> depending on what else is happening all sorts of
> things can go wrong.

That's true but it is nothing new. Window procedures are often reentered.
It's why, by the way, that I suggested that the OP might want to disable
some UI elements before spinning the loop. Further, the OP was looking for
the equivalent of VB's DoEvents(). That's what I sketched.

> If you want to pause execution, why don't you break your routine into two
> pieces, and set a timer for the amount of time you want to pause.
> Then in your timer handler, post a message back to your window to schedule
> the second half of the task.

The OP had the pause by Sleep()-ing. The reason he posted the question was
that he needed to process messages while waiting.

Regards,
Will
Frank - 16 Jan 2007 21:13 GMT
> It's not dangerous per se.
>
>> Your program becomes reentrant at this point, so
>> depending on what else is happening all sorts of
>> things can go wrong.

I know I can't expect people to spend much time giving tutorials in a NG but
I wonder if you can just give a brief explanation of what the above means.

What should I be looking for in my program that might make this approach
dangerous?

Thanks
Ben Voigt - 16 Jan 2007 21:56 GMT
>> It's not dangerous per se.
>>
[quoted text clipped - 8 lines]
> What should I be looking for in my program that might make this approach
> dangerous?

Reentrant:

void A() {
...
DoEvents(); // -> this might call A()
...
}

Trouble:

int i;
void B(array<T> j) {
   for( i = 0; i < j.Length; i++ ) {
       DoEvents();    // this might change i
       j[i].ToString();    // which could cause an array index bounds
exception here
   }
}
Frank - 16 Jan 2007 22:21 GMT
Thanks for the insight

>>> It's not dangerous per se.
>>>
[quoted text clipped - 27 lines]
>    }
> }
William DePalo [MVP VC++] - 17 Jan 2007 04:26 GMT
> I know I can't expect people to spend much time giving tutorials in a NG
> but I wonder if you can just give a brief explanation of what the above
> means.
>
> What should I be looking for in my program that might make this approach
> dangerous?

You have to understand what your application is doing. :-)

Seriously, say for example, that you have an application with a menu which
has an item that causes a function to run. Say that it is in that function
where you intend to put the little "wait" snippet that I sketched. If you
did nothing else but insert that snippet, it would be possible for the user
to pull down the menu and click the menu item which would cause the function
to be called a second time before the first call returns.

That would be bad but it is not the end of the world. The simple fix in a
case like that would be to disable the menu item, run the function and then
reenable it.

It's not at all unusual in places where you find such secondary message
loops to find some disabling of UI widgets before the loop runs followed by
reenabling afterwards. Exactly what kind of fiddling you need to do depends
on exactly what your application does.

Regards,
Will
Frank - 17 Jan 2007 08:04 GMT
I my situation the user could not cause reentrant.
Thanks for the explanation

>> I know I can't expect people to spend much time giving tutorials in a NG
>> but I wonder if you can just give a brief explanation of what the above
[quoted text clipped - 23 lines]
> Regards,
> Will
A Wieser - 17 Jan 2007 08:24 GMT
|I my situation the user could not cause reentrant.
| Thanks for the explanation

Are you sure?  Press ALT+F4 while in your delayed portion.

Anthony Wieser
Wieser Software Ltd
Frank - 17 Jan 2007 13:48 GMT
What is Alt+F4.  Is this it?
ALT+F4 (Close the active item, or quit the active program)

Sure didn't close my program.

I tried it but I have to press the spacebar and then Alt+F4 within one tenth
of a second.

Once I did see something on the screen but am not sure what caused it.

Thanks

> |I my situation the user could not cause reentrant.
> | Thanks for the explanation
[quoted text clipped - 3 lines]
> Anthony Wieser
> Wieser Software Ltd
A Wieser - 18 Jan 2007 06:51 GMT
| > Are you sure?  Press ALT+F4 while in your delayed portion.
| >
| > Anthony Wieser
| > Wieser Software Ltd

| What is Alt+F4.  Is this it?
| ALT+F4 (Close the active item, or quit the active program)
[quoted text clipped - 5 lines]
|
| Once I did see something on the screen but am not sure what caused it.

My prediction was that your program would process the shutdown message
WM_CLOSE, and tidy up, then destroy the window
But unfortunately, when your routine finished waiting, it would find that
all of its objects had been destroyed by the shutdown.

I can't predict what circumstance prevented that, though.

Anthony Wieser
Wieser Software Ltd
Frank - 18 Jan 2007 16:05 GMT
thanks for taking time to help

>| > Are you sure?  Press ALT+F4 while in your delayed portion.
> | >
[quoted text clipped - 21 lines]
> Anthony Wieser
> Wieser Software Ltd
Frank - 15 Jan 2007 21:00 GMT
This is native code.
I'm looping and within the loop this would work
Or receive the messages while sleeping - either way would be OK
.
.
.
sleep(100)
allow key board messages
.
.
.

>> I'm using  Sleep(100) and that works OK.
>>
[quoted text clipped - 8 lines]
> Regards,
> Will

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.