Saturday, September 29, 2007

Sleep in VB.NET and C#

The sleep is sometimes hard to find in .NET. Here is an example for VB.NET and C#:

VB.NET

Imports System.Threading
.
.
.

Dim waitTime as Integer = 300

Thread.Sleep(waitTime)


C#

using System.Threading;
.
.
.
int waitTime = 300;

Thread.Sleep(waitTime);


The Sleep method can be used with both an integer and a TimeSpan parameter. An integer value can be used to specify the wait time in milliseconds, or the wait time can be specified as a TimeSpan like this:

In VB.NET:

Dim waitTime As New TimeSpan(0, 0, 0, 0, 300)

or in C#:

TimeSpan waitTime = new TimeSpan(0, 0, 0, 0, 300);


There is also a pretty good example at the following web site:

http://msdn.microsoft.com/en-us/library/system.threading.thread.sleep(VS.71).aspx

1 comment:

Anonymous said...

You don't need CurrentThread. System.Threading.Thread.Sleep should suffice.