Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sleep Without Freezing in VB.Net

Status
Not open for further replies.

ianwin

Programmer
Jul 5, 2005
44
US
I need to make a VB.Net application pause for 5 seconds but still be responsive. I tried using a loop with a timeout based on a timer with DoEvents in the loop, this worked but used 99% of the cpu which was not acceptable. I have changed this to using System.Threading.Sleep(5000) which reduces the CPU usage however the application then becomes unresponsive when the user attempts to interact with it.

Is there a way I can pause the program in the way described and have the application still responsive?

-Ian
 
To be honest I think you are going about this the wrong way.

Why do you want it to wait? For what is it waiting?

Christiaan Baes
Belgium

"My old site" - Me
 
I need the program to wait because it is interacting with another system by writing to and from a text file.

The program writes a command in the text file with the other application reads, and executes before writing the results back into the same text file.

My application cannot continue until it has the results of the command execution and it needs to pause to allow this to be completed.
 
So how does it know when the other system has completed?

And why do you need the system/your program to be still responsive? Because you want the user to be able to interrupt the process?

Then I would suggest running it in it's own thread.

Christiaan Baes
Belgium

"My old site" - Me
 
I would also recommend using a separate thread.
However, you can also place the sleep in a loop.
The method below will prevent excessive use of the cpu, and the program will still be responsive.
Code:
    Private Sub Snooze(ByVal seconds As Integer)
        For i As Integer = 0 To seconds * 100
            System.Threading.Thread.Sleep(10)
            Application.DoEvents()
        Next
    End Sub

 
I will try putting the loop but I am also interested in how to run this as a seperate thread. The main application has several controls on which represent devices around our site that the program communicates with via the text file described above. There is no direct interaction between the application and the devices therefore the application write a command to the text file then waits 5 seconds and attempts to read the results from the text file. If a conflict is encountered when reading the file this means one of the devices is still writing to it and the program backs off for another 5 seconds before attempting to read again.

while this is going on there are other controls on the screen for reporting on information which the user still needs to access, this is why there is a need for a pause and for the program to still be responsive.
 
Yep a typical use for threads. You must just take care that controls are not threadsafe and that you therefor have to use a threadsafe way in which to update them.

If you don't know how threads work then look up backgroundworker it tries to make things easier.

BTW: a thread runs in the background while your form also runs a the same time (more or less) So the thread that checks the file will just run and do it's thing while the user can still use the form. The thread/method can update your form if needed and when it has a new value. In this case you could use the timer that is in system.thread.

Christiaan Baes
Belgium

"My old site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top