Async sleep

In VB.Net, if I use System.Threading.Thread.Sleep(5000), then my application is halted. Is the a way to prevent this. While Sleep(5000) is executing, other controls like textbox or button still can active ?
[207 byte] By [TanChenYee] at [2007-12-16]
# 1
No.

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

DanielMoth at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2

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.

IlyaTumanov at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 3

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 !

Tan.NetCF at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 4
Your "VB Sleep" is not the equivalent of Thread.Sleep. In fact that VB funciton is pretty ugly and I would not try to bring it forward in the dotnet world. Just FYI, DoEvents is still available via Application.DoEvents

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

DanielMoth at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 5

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

SingingintheRain at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 6

ASP is off topic here. Please post to ASP.Net forums instead.

IlyaTumanov at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...