Thread.Sleep will block the thread and it will not be able to process any other work. It sounds like you are doing this in the main thread and hence your controls become unresponsive. If you did it in another thread, then that thread would sleep and your UI thread would process windows messages and hence your controls would be usable. Why are you calling Sleep?
Now take a large step back and describe to us what it is you are trying to achieve (in end user terms).
Cheers
Daniel
Asynchronous Sleep() makes no sense, it’s the equivalent of doing nothing at all.
The point of Sleep() is to halt execution for some time, if you want execution to continue, simply don't call Sleep(). If you need to do some job from time to time, you can use timers for that.
Public Sub Sleep(sglSecond As Single)
Dim lngTimeOut As Long
Dim sglLastTick As Single
On Error GoTo ERROR
lngTimeOut = sglSecond * 1000
sglLastTick = Timer
Do Until glbblnWakeUp = True
Select Case MsgWaitForMultipleObjects(0, 0, False, lngTimeOut, 255)
Case 0
DoEvents
lngTimeOut = ((sglSecond) - (Timer - sglLastTick)) * 1000
If lngTimeOut < 0 Then lngTimeOut = 0
Case Else
Exit Do
End Select
Loop
glbblnWakeUp = False
Exit Sub
ERROR:
glbblnWakeUp = False
SetLog "ERROR In setting sleep: " & Err.Description
End Sub
Above is the code I have in VB6. I was trying to convert to VB.Net. However, I think Timer in VB.Net is different than Timer in VB6. Here is what I am doing:
Private Sub Login()
......
......
......
Send data to server.
Display "Please wait"
Sleep (60000) - Async sleep (above Sleep())
If blnReceivedReponse then
Display "You have received response"
Else
Display "Failed"
End Id
........
........
End Sub
When I received from the server, I set blnReceived to True. I need to do the Sleep
in this way. Can you help me to convert Sleep() in VB6 to VB.Net ?
Thanks !
What is the "Send Data to server"? I guess some kind of asyn operation such as using sockets or serialport etc. So you need to have two methods: SendData, ReceivedData.
From you Login (or user pressing button or whatever) you call SendData which has the code up to your "Sleep" method *without* the call to Sleep. When the network operation completes, make sure it calls ReceivedData and in there place the code you have after the "Sleep". Your VB6 code has merged the two distinct operations into one and holds them together with a hacky sleep method.
In other words, you should use event driven programming. You make a call, and later you get a callback (usually implemented as an event). Between those two operations you show a progressbar or busy cursor etc to the user.
A couple of links that may help (although I suggest you get a book to get up to speed with dotnet, asynchronous/eventDrived programming):
http://www.danielmoth.com/Blog/2004/11/dont-poll.html
http://www.danielmoth.com/Blog/2004/12/backgroundworker-sample.html
Cheers
Daniel
Hi Daniel
I am hoping you can help me with a similar issue in ASP.NET. I have an optimization program external to my web based UI that get kicked off as a separate process, and take any where from 5-30 min to compelte. The optimization program writes its progress to an SQL DB. What I would like to do in the web UI is plot a graph of the optimization progress. I can do this manually with a button on the UI that updates the graph whenever the user clicks on the button. What I would like to have is a thread/process/timer that forces an update of the graph every X seconds. I have tried threads, processes, and backgroundworker, but these all seem to run on the server.
Any suggestions?
Regards
Trevor Miles