Well im trying to "sleep" or make the program twiddle its thumbs for 15 seconds before continueing but without pausing the main thread.

So i thought maybe if i create a new thread and Sleep in that one that would help but no.

So then i thought the timer object but still no.

Now i dont know if the above "no"'s are because im completely stupid and cant use them or they didnt actually work.....

Heres the new thread method i stole from microsoft.

class Sleep
{
    static TimeSpan waitTime = new TimeSpan(0, 0, 15);

    public static void sSleep()
    {
        Thread newThread = new Thread(new ThreadStart(Sleeping));
        newThread.Start();

        if (newThread.Join(waitTime + waitTime))
        {
            //
        }
        else
        {
            //
        }
    }
    static void Sleeping()
    {
        Thread.Sleep(waitTime);
    }
}

That didnt work for me (it paused the main thread)

If someone could show me how to use the timer correctly that would be much appriciated.

Recommended Answers

All 5 Replies

You could just use a timer.

You could just use a timer.

as i said if someone could show me how to use the timer correctly that would be very very helpful :)

I don't understand your problem. You're trying to make your program 'sleep' without pausing the main thread? That's not really a sleep because sleep would be a blocking call. If what you need is a timer, then use the Timer class. Here is how it looks in short:

Timer mTimer = new Timer(TIMEOUT_VALUE);
mTimer.Elapsed += new ElapsedEventHandler(TIMEOUT_METHOD);
mTimer.Start();

Here is the signature for TIMEOUT_METHOD

public void MyMethod(object source, ElapsedEventArgs e)
{ }

This method will execute when your timer ends. The timer will then restart automatically when the call to this method completes.

Timer is also a component on your toolbar - with other examples on how to use it in your helpfile.

Perhaps if you let the main thread spawn a new thread. You could pause the new thread and let the main thread continue?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.