Question Pause a Loop

shibity

New member
Joined
Jan 24, 2009
Messages
1
Programming Experience
Beginner
Hey guys, im new here but so far it looks like a good place. Right now i need help with my program. I have it looping and im trying to pause the loop for several seconds. Heres my code
VB.NET:
For x = 1 To Text3.Text
If Check1.Value = 1 Then
SendKeys Text1.Text
SendKeys "{Enter}"
End If
Sleep (200)
If Check2.Value = 1 Then
SendKeys Text2.Text
SendKeys "{Enter}"
End If
Now sleep workd, But what it did was pause the whole program, instead of pausing between each loop. Any ideas,
Thanks,
Dave
 
Now sleep workd, But what it did was pause the whole program, instead of pausing between each loop. Any ideas,
Thanks,
Dave

That's because your program only has one thread, that thread is running the loop code, and sleep slepps the thread therefore sleeping the loop code but also the whole program

You need to alter your thinking to be event based rather than loop based. You seem to initiate an action only when a checkbox is checked, so attach an event handler to its checkedchanged event!

Also if you must do something, then wait several seconds, then do something else, start a timer and after X seconds have elapsed its Tick event will fire, and in the Tick event handler you can do your subsequent processing


-

You can also use a BackgroundWorker, which has a separate thread. You can sleep that without upsetting the main program, but then you open the Multithreading can of worms.. Additionally, I tend to recommend AVOIDING use of thread.sleep for situations other than simulating heavy load
 
Additionally, I tend to recommend AVOIDING use of thread.sleep for situations other than simulating heavy load
Why? You often need to in polling loops that that don't need to go CPU berserk and ask for info every nanosecond, but perhaps could benefit of sleeping 500ms each loop, you'd still have fast reliable response at least twice a second. You may not want to let any bit of this polling work to take place in UI thread as with a Timer. Yes, you could use a Threading.Timer that callback on a new Threadpool thread each time, but in this case using a single long running thread is better thread management, it could also avoid sync problems for multiple threads in case poll gave result.
 
That post mostly talks about sleeping UI thread is bad (or COM STA threads), I agree, but that is not the scenario here. The argumentation about create/destroy time is actually an argument for using Sleep for long running threads, and avoid creating other threads. With "A WinForm thread must be STA" he means UI thread. "STA" actually only has a meaning when using COM and only if a COM call is made is the thread initialized for STA. So a thread may also be requested for STA, but may not enter that apartment. New threads defaults to MTA by the way, and is the case discussed here. Message pumps also have to be created, a thread does not start one by itself. A winforms application is configured to start one by default, the application startup thread is also marked STA for measures. If you run UI elements in other threads they communicate via main message loop, either in MTA or in STA or across MTA/STA border. Only if a thread creates windows is a message queue created for the thread. I don't see any arguments against using Sleep in a polling scenario as described, nor any better solution proposed. Sleep is not used for timing. A WaitHandle timeout will use a threadpool wait thread, why call another idle thread or have a new thread created multiple times a second when you only need one thread? Sleep is not a never-never as I see it.
 
Back
Top