Is there a better way to "Wait"?

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

I wanted to do this "inline" and not use a timer. Is there a better way?

Do
zipError = UnzipFile(datPath & zipFileName, datPath, False)
If zipError = "Success" Then Exit Do
UpdateStatus("File in use, waiting 5 minutes")
Wait(5)
Loop

Public Sub Wait(ByVal minutes As Integer)
Dim startTime As Integer = Minute(Now)
Dim curTime As Integer
Do
curTime = Minute(Now)
If curTime < startTime Then curTime += 60
If curTime = startTime + minutes Then Exit Sub
Application.DoEvents()
Loop
End Sub
 
Yes , there is a better way.
System.Threading.Thread.Sleep(new TimeSpan(0, 5, 0))


~Steve
 
Yes , there is a better way.
System.Threading.Thread.Sleep(new TimeSpan(0, 5, 0))


~Steve

Sure, if you want your application to be locked up for 5 minutes... :)
 
I wanted to do this "inline" and not use a timer. Is there a better way?

Do
zipError = UnzipFile(datPath & zipFileName, datPath, False)
If zipError = "Success" Then Exit Do
UpdateStatus("File in use, waiting 5 minutes")
Wait(5)
Loop

Public Sub Wait(ByVal minutes As Integer)
Dim startTime As Integer = Minute(Now)
Dim curTime As Integer
Do
curTime = Minute(Now)
If curTime < startTime Then curTime += 60
If curTime = startTime + minutes Then Exit Sub
Application.DoEvents()
Loop
End Sub

As has been mentioned, there is System.Threading.Sleep - but I wouldn't use
that for extended periods of time or someones going to give your
application the 3-finger salute... You might want to use a timer control
or a System.Threading.Timer to reschedule your method.
 
Terry,

When it is 5 minutes I would not use that long time I would give the user
some information how long it is to go.

That can be done either by the treading sleep or with one of the timers what
I prefer (for this form handling I prefer the forms.form.timer because it is
so easy to use and this is very easy timer handling).

To use the time sleep (all types here and not tested so nothing more as
almost complete pseudo)
\\\
Dim frm as new SplashForm
For i as integer = 1 to 100
frm.label = i * 300 ('you may translate it yourself nice using the
timespan)
frm.show
thread.threading.sleep(1000)
Next
frm.close
////
With the forms.form.timer event it is even easier to do.
(mostly is forgotten to enable that and set the correct wait time)

I hope this helps?

Cor
 
One error I saw beside the types (typed) the wait time in this sample
should be 3000 what is 3 seconds what is the normal time a user does not
recognise as wait time.

Cor
 
* "Terry Olsen said:
I wanted to do this "inline" and not use a timer. Is there a better way?

'System.Threading.Thread.Sleep'. If you are using a Windows Forms
application, this will block the app's main UI thread. To avoid that,
perform the waiting in a separate thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top