473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
Jan 15 '07 #1
18 1775
" Frank" <fr***@a-znet.comwrote in message
news:em*************@TK2MSFTNGP06.phx.gbl...
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?
Is it in a managed or native application that you need to do that?

Regards,
Will
Jan 15 '07 #2
Native
"William DePalo [MVP VC++]" <wi***********@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>" Frank" <fr***@a-znet.comwrote in message
news:em*************@TK2MSFTNGP06.phx.gbl...
>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?

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

Regards,
Will

Jan 15 '07 #3
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
..
..
..

"William DePalo [MVP VC++]" <wi***********@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>" Frank" <fr***@a-znet.comwrote in message
news:em*************@TK2MSFTNGP06.phx.gbl...
>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?

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

Regards,
Will

Jan 15 '07 #4
" Frank" <fr***@a-znet.comwrote in message
news:uD**************@TK2MSFTNGP06.phx.gbl...
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
Jan 15 '07 #5
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

"William DePalo [MVP VC++]" <wi***********@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>" Frank" <fr***@a-znet.comwrote in message
news:uD**************@TK2MSFTNGP06.phx.gbl...
>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


Jan 15 '07 #6
" Frank" <fr***@a-znet.comwrote in message
news:O5**************@TK2MSFTNGP04.phx.gbl...
>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
Jan 15 '07 #7
Took 10 minutes, I had to look up some things but basically dropped your
code in and it worked great.

Thanks

"William DePalo [MVP VC++]" <wi***********@mvps.orgwrote in message
news:eV**************@TK2MSFTNGP03.phx.gbl...
>" Frank" <fr***@a-znet.comwrote in message
news:O5**************@TK2MSFTNGP04.phx.gbl...
>>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


Jan 16 '07 #8

"William DePalo [MVP VC++]" <wi***********@mvps.orgwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
|" Frank" <fr***@a-znet.comwrote in message
| news:uD**************@TK2MSFTNGP06.phx.gbl...
| 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
|
|

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

Jan 16 '07 #9
"A Wieser" <me@wieser.plush.comwrote in message
news:eB*************@TK2MSFTNGP04.phx.gbl...
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
Jan 16 '07 #10
>
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

Jan 16 '07 #11

" Frank" <fr***@a-znet.comwrote in message
news:ey**************@TK2MSFTNGP04.phx.gbl...
>
>>
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?
Reentrant:

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

Trouble:

int i;
void B(array<Tj) {
for( i = 0; i < j.Length; i++ ) {
DoEvents(); // this might change i
j[i].ToString(); // which could cause an array index bounds
exception here
}
}
Jan 16 '07 #12
Thanks for the insight
"Ben Voigt" <rb*@nospam.nospamwrote in message
news:ei**************@TK2MSFTNGP03.phx.gbl...
>
" Frank" <fr***@a-znet.comwrote in message
news:ey**************@TK2MSFTNGP04.phx.gbl...
>>
>>>
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?

Reentrant:

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

Trouble:

int i;
void B(array<Tj) {
for( i = 0; i < j.Length; i++ ) {
DoEvents(); // this might change i
j[i].ToString(); // which could cause an array index bounds
exception here
}
}

Jan 16 '07 #13
" Frank" <fr***@a-znet.comwrote in message
news:ey**************@TK2MSFTNGP04.phx.gbl...
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
Jan 17 '07 #14
I my situation the user could not cause reentrant.
Thanks for the explanation
"William DePalo [MVP VC++]" <wi***********@mvps.orgwrote in message
news:uT****************@TK2MSFTNGP03.phx.gbl...
>" Frank" <fr***@a-znet.comwrote in message
news:ey**************@TK2MSFTNGP04.phx.gbl...
>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

Jan 17 '07 #15

" Frank" <fr***@a-znet.comwrote in message
news:Ow**************@TK2MSFTNGP03.phx.gbl...
|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

Jan 17 '07 #16
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

"A Wieser" <me@wieser.plush.comwrote in message
news:Or**************@TK2MSFTNGP02.phx.gbl...
>
" Frank" <fr***@a-znet.comwrote in message
news:Ow**************@TK2MSFTNGP03.phx.gbl...
|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

Jan 17 '07 #17
| Are you sure? Press ALT+F4 while in your delayed portion.
| >
| Anthony Wieser
| Wieser Software Ltd
| >
|
" Frank" <fr***@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
| 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.
|
|

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

Jan 18 '07 #18
thanks for taking time to help

"A Wieser" <me@wieser.plush.comwrote in message
news:ed*************@TK2MSFTNGP02.phx.gbl...
>| Are you sure? Press ALT+F4 while in your delayed portion.
| >
| Anthony Wieser
| Wieser Software Ltd
| >
|
" Frank" <fr***@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
| 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.
|
|

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

Jan 18 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
3
by: jdph40 | last post by:
In Access 2002, I designed a simple database for our Safety department to enter results of a survey. There are 41 true/false statements. I have a main form called frmSurvey with a subform called...
19
by: C# Learner | last post by:
What's a nice way to create a non-blocking pause in execution? Will I need to use some kind of timer for this, or is there a nicer way?
8
by: Wim | last post by:
My GUI application starts a process (a console program) when the user hits Play. I would like to add an option to pause that process. The code I've added to detect if the user hit pause/unpause...
38
by: Jackie | last post by:
I just want the programme to stop for a while. Thanks.
6
by: Peted | last post by:
Hi wondering what is the best way to do this Need a user to click a button, that sends 3 or 4 string based commands via a TCP/ip socket link I can connect to the ip device no problems, am...
0
by: steveyjg | last post by:
I'm trying to call a function that will pause execution for 30 seconds and have a counter timer counting to 30 seconds during this pause. When the counter hits 30 execution of the program will...
4
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= | last post by:
I need to pause the action of my code for a quarter second or so. What can I use to do this? Thanks! Bob
1
by: desktop | last post by:
Is there someway to pause the execution of a program? I have a large while loop and before it gets started I would like to pause the program so I can see the output before it gets overwritten by...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.