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? 19 86652
int pauseTime = 2000;
System.Threading.Thread.Sleep(pauseTime);
"C# Learner" <cs****@learner.here> wrote in message
news:8b********************************@4ax.com... 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?
"Vadym Stetsyak" <pd****@ukr.net> wrote: "C# Learner" <cs****@learner.here> wrote in message news:8b********************************@4ax.com.. . 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?
int pauseTime = 2000; System.Threading.Thread.Sleep(pauseTime);
Hi Vadym, thanks for your quick response.
The problem with that is - it blocks the current (GUI) thread. I'm
looking for something that will do this without blocking the current
thread.
Regards
C# Learner <cs****@learner.here> wrote: The problem with that is - it blocks the current (GUI) thread. I'm looking for something that will do this without blocking the current thread.
What *exactly* are you trying to pause then? If it's another thread,
just make that other thread sleep instead.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
In article <fa********************************@4ax.com>, cs****@learner.here says... "Vadym Stetsyak" <pd****@ukr.net> wrote:
"C# Learner" <cs****@learner.here> wrote in message news:8b********************************@4ax.com.. . 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?
int pauseTime = 2000; System.Threading.Thread.Sleep(pauseTime);
Hi Vadym, thanks for your quick response.
The problem with that is - it blocks the current (GUI) thread. I'm looking for something that will do this without blocking the current thread.
Regards
You'll have to use a separate thread for the GUI and the thing you want
to pause then use Vadym's solution. At first I wasn't sure what you
meant by non-blocking, in relation to other programs or within your
program,
--
Thanks
Mark mm
Jon Skeet [C# MVP] <sk***@pobox.com> wrote: C# Learner <cs****@learner.here> wrote: The problem with that is - it blocks the current (GUI) thread. I'm looking for something that will do this without blocking the current thread.
What *exactly* are you trying to pause then? If it's another thread, just make that other thread sleep instead.
Let me clarify...
My GUI app has only a single thread. I would like to create a pause
in execution without blocking this single thread.
Right now I'm using a timer to do this:
private void Foo()
{
delayTimer.Start();
}
private void delayTimer_Tick(object sender, System.EventArgs e)
{
delayTimer.Stop();
DoSomething();
}
The effect here is that DoSomething() is called after a short delay.
Now, this works fine, but I was wondering if there's a nicer method of
doing this.
Thanks
C# Learner <cs****@learner.here> wrote: Let me clarify...
My GUI app has only a single thread. I would like to create a pause in execution without blocking this single thread.
I still don't understand what you really mean by a "pause" then. A
pause has to occur in *some* thread...
Right now I'm using a timer to do this:
private void Foo() { delayTimer.Start(); }
private void delayTimer_Tick(object sender, System.EventArgs e) { delayTimer.Stop(); DoSomething(); }
The effect here is that DoSomething() is called after a short delay.
Now, this works fine, but I was wondering if there's a nicer method of doing this.
That sounds like it's basically another thread (a threadpool thread)
which is then calling back to your event handler. (I don't know whether
it's a System.Windows.Forms.Timer or a System.Threading.Timer though.)
That's basically what you need though - it's not what I'd describe as a
pause though, so much as scheduling something to occur at some point in
the future. You're not actually *pausing* anything, as far as I can
see.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... C# Learner <cs****@learner.here> wrote: The problem with that is - it blocks the current (GUI) thread. I'm looking for something that will do this without blocking the current thread.
What *exactly* are you trying to pause then? If it's another thread, just make that other thread sleep instead.
I think he wants to make the main thread to go to sleep for a number of
seconds yet wake up and service GUI events as they occur, then get back to
sleep at the point it was initially laid to bed and wait for the remaining
time to be waken up by the alarm set when it was first put to sleep. Just
getting a grip on threads I guess :-).
I agree, he should explain the problem, someone will post a solution and
threads will make more sense to him.
Coming from a non-threaded background this is the way you think, I can
relate to that. In the old days we used to keep our system responsive by
doing our "background processing" on the one main thread in a loop, calling
Application.ProcessMessages or the equivalent of that every x iterations so
the user could abort the operation by pressing a button that raised a flag
that would make the code leave the loop.
Martin.
"Martin Maat [EBL]" <du***@somewhere.nl> wrote: I think he wants to make the main thread to go to sleep for a number of seconds yet wake up and service GUI events as they occur, then get back to sleep at the point it was initially laid to bed and wait for the remaining time to be waken up by the alarm set when it was first put to sleep. Just getting a grip on threads I guess :-).
I agree, he should explain the problem, someone will post a solution and threads will make more sense to him.
I'm just thinking to make a simple pause without going to the extra
effort of writing and maintaining a seperate thread. Sure, I know how
this could be implemented with an extra thread, but I really want a
simple and neat solution here.
Never mind - I'll stick with the timer.
Coming from a non-threaded background this is the way you think, I can relate to that. In the old days we used to keep our system responsive by doing our "background processing" on the one main thread in a loop, calling Application.ProcessMessages or the equivalent of that every x iterations so the user could abort the operation by pressing a button that raised a flag that would make the code leave the loop.
I've seen a lot of recent code that does this, believe it or not!
It's almost an abuse of the CPU.
Martin.
C# Learner <cs****@learner.here> wrote: I'm just thinking to make a simple pause without going to the extra effort of writing and maintaining a seperate thread. Sure, I know how this could be implemented with an extra thread, but I really want a simple and neat solution here.
Okay, so "pause" was a bad choice of word here. I'm thinking of
"pause" from a high-level -- from the user's perspective.
> My GUI app has only a single thread. I would like to create a pause in execution without blocking this single thread.
If we take this literally it is impossible, contradictary in itself. But if
lagged execution of a command is really what you are after your solution is
effective, efficient and elegant.
The only application I can think of is an OS/2 or Linux GUI simulator.
"Sluggish responsiveness for Windows". Might that be what you are making?
:-).
Martin.
"Martin Maat [EBL]" <du***@somewhere.nl> wrote: My GUI app has only a single thread. I would like to create a pause in execution without blocking this single thread. If we take this literally it is impossible, contradictary in itself. But if lagged execution of a command is really what you are after your solution is effective, efficient and elegant.
The only application I can think of is an OS/2 or Linux GUI simulator. "Sluggish responsiveness for Windows". Might that be what you are making? :-).
I can't speak for OS/2 'coz I've never used it, but I find GNU/Linux
faster in general (on this box) that Win XP.
Anyway...
I'm making a simple game in which the human makes his/her move, then
the computer opponent makes "his/her" move after a slight delay.
Martin.
Jon Skeet [C# MVP] <sk***@pobox.com> wrote: That sounds like it's basically another thread (a threadpool thread) which is then calling back to your event handler. (I don't know whether it's a System.Windows.Forms.Timer or a System.Threading.Timer though.)
It's a System.Windows.Forms.Timer timer. Hmm... I might look into
using one of the other two timers, as this one is eligable for garbage
collection when it's disabled. Can't be good for this app - it's
starting a stopping a lot.
That's basically what you need though - it's not what I'd describe as a pause though, so much as scheduling something to occur at some point in the future. You're not actually *pausing* anything, as far as I can see.
C# Learner <cs****@learner.here> wrote: It's a System.Windows.Forms.Timer timer. Hmm... I might look into using one of the other two timers, as this one is eligable for garbage collection when it's disabled.
I'm presuming that means the Win32 timer this object wraps around.
> It's a System.Windows.Forms.Timer timer. Hmm... I might look into using one of the other two timers, as this one is eligable for garbage collection when it's disabled. Can't be good for this app - it's starting a stopping a lot.
As long as your timer component does not run out of scope it will not be
garbage collected, I see no need to worry there.
Furthermore, I am not absolutely sure about how this particular timer
component is implemented but its behavior suggests it is the good old 18.2
times a second interupt based timer. I do not expect a separate thread to be
living in the timer component. Every 0.054 seconds your timer gets a chance
to check if the interval has elapsed and if it has the component will post a
message to your form which is handled by your main thread like any other UI
event and dispatched to your timer event handler. In a GUI application you
cannot get more efficient than that.
Martin.
> >The only application I can think of is an OS/2 or Linux GUI simulator. "Sluggish responsiveness for Windows". Might that be what you are making? :-). I can't speak for OS/2 'coz I've never used it, but I find GNU/Linux faster in general (on this box) that Win XP.
I installed Mandrake with KDE and it looks very nice, I don't want
to bash it in any way but I found it considerably less snappy than
Windows XP on the same box.
Anyway...
Indeed.
I'm making a simple game in which the human makes his/her move, then the computer opponent makes "his/her" move after a slight delay.
I would use a timer in the same manner, seems like the way to go, no need to
mess with threads. I might add that if the computer's thinking is (or might
get as the app evolves) a heavy task that takes a bit of processing time,
you will need that thread anyway for determining the computer's next move.
And then I would say it would be better to start working on the response
right after the human made his move and then either make the computed move
straight away or, if the result would be available before the minimum lapse,
sleep until the minimum lapse has passed. That way you would not waste any
time.
Martin.
"Martin Maat [EBL]" <du***@somewhere.nl> wrote: It's a System.Windows.Forms.Timer timer. Hmm... I might look into using one of the other two timers, as this one is eligable for garbage collection when it's disabled. Can't be good for this app - it's starting a stopping a lot. As long as your timer component does not run out of scope it will not be garbage collected, I see no need to worry there.
I wouldn't have been concernced here, but then I read this: http://msdn.microsoft.com/library/de...thodsTopic.asp
"A timer that is disabled is subject to garbage collection."
I wonder what this means exactly.
Furthermore, I am not absolutely sure about how this particular timer component is implemented but its behavior suggests it is the good old 18.2 times a second interupt based timer. I do not expect a separate thread to be living in the timer component. Every 0.054 seconds your timer gets a chance to check if the interval has elapsed and if it has the component will post a message to your form which is handled by your main thread like any other UI event and dispatched to your timer event handler. In a GUI application you cannot get more efficient than that.
I expect it to work like this too.
Martin.
Regards
"Martin Maat [EBL]" <du***@somewhere.nl> wrote: >The only application I can think of is an OS/2 or Linux GUI simulator. >"Sluggish responsiveness for Windows". Might that be what you are making? >:-). I can't speak for OS/2 'coz I've never used it, but I find GNU/Linux faster in general (on this box) that Win XP. I installed Mandrake with KDE and it looks very nice, I don't want to bash it in any way but I found it considerably less snappy than Windows XP on the same box.
I'm a Mandrake user myself.
:-) Anyway...
Indeed.
I'm making a simple game in which the human makes his/her move, then the computer opponent makes "his/her" move after a slight delay.
I would use a timer in the same manner, seems like the way to go, no need to mess with threads. I might add that if the computer's thinking is (or might get as the app evolves) a heavy task that takes a bit of processing time, you will need that thread anyway for determining the computer's next move. And then I would say it would be better to start working on the response right after the human made his move and then either make the computed move straight away or, if the result would be available before the minimum lapse, sleep until the minimum lapse has passed. That way you would not waste any time.
Well, in this app, the computer's thinking is so simple that its move
would be made more or less *immediately* after the human's move, so I
don't have to concern myself with the above.
Martin.
Thanks Martin.
> >As long as your timer component does not run out of scope it will not be garbage collected, I see no need to worry there. I wouldn't have been concernced here, but then I read this:
http://msdn.microsoft.com/library/de...thodsTopic.asp "A timer that is disabled is subject to garbage collection."
I don't find that statement behind the link. The same message in different
words is here though: http://msdn.microsoft.com/library/de...abledtopic.asp
"The timer is not subject to garbage collection when the value is true."
I wonder what this means exactly.
I would think this simply means there is some lazy initialization and
agressive cleanup going on within the timer object which is after all a
wrapper around a system timer. Since system timers are a limited resource
and you are not really using the system timer while the component's Enabled
property is set to false it can be released. As soon as you enable it again
you will get a new one on the fly. To you as a client it makes no
difference. I cannot imagine that the wrapping timer component you have on
your form will be spontaniously garbage collected while you are holding a
reference to it. The choice of words in the ducumentation may be
unfortunate.
I am making this up though, I too am getting curious about the true meaning
of the statement.
Martin.
"Martin Maat [EBL]" <du***@somewhere.nl> wrote: >As long as your timer component does not run out of scope it will not be >garbage collected, I see no need to worry there. I wouldn't have been concernced here, but then I read this:
http://msdn.microsoft.com/library/de...thodsTopic.asp "A timer that is disabled is subject to garbage collection."
I don't find that statement behind the link. The same message in different words is here though:
http://msdn.microsoft.com/library/de...abledtopic.asp
"The timer is not subject to garbage collection when the value is true."
Sorry, the correct link should have been: http://msdn.microsoft.com/library/en...asp?frame=true I wonder what this means exactly.
I would think this simply means there is some lazy initialization and agressive cleanup going on within the timer object which is after all a wrapper around a system timer. Since system timers are a limited resource and you are not really using the system timer while the component's Enabled property is set to false it can be released. As soon as you enable it again you will get a new one on the fly. To you as a client it makes no difference. I cannot imagine that the wrapping timer component you have on your form will be spontaniously garbage collected while you are holding a reference to it. The choice of words in the ducumentation may be unfortunate.
I am making this up though, I too am getting curious about the true meaning of the statement.
The bit that throws me is that it says that the "timer" is subject to
garbage collection when disabled. Ambiguous...
Martin.
Thanks for your thoughts! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dr. Know |
last post by:
I am working on an ASP page that writes to several databases, ranging
from MDBs to x-base.
One of the tasks involves using an existing highest value from the DB
and incrementing it before...
|
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...
|
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...
|
by: Fred Nelson |
last post by:
Hi:
I'm trying to write a routine that will permit my VB.NET program to
pause execution for five minutes and try a task again if there is a
failure. After a set number of tries it should return...
|
by: Noggon |
last post by:
Maybe I'm stupid, but I can't find out what is wrong, can anyone help?
I keep getting the function first as being undefined for some reason I
don't get.
I'm trying to use setTimeout() to pause...
| |
by: Frank |
last post by:
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...
|
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...
|
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...
|
by: npcarp |
last post by:
Is this hard?
I am introducing Python to my students and want to make a simple, interactive program that will, while it's running, 'listen' for any keyboard events. That is, I don't want to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |