Timeout Class
Collapse the table of content
Expand the table of content

Timeout Class


Contains constants that specify infinite time-out intervals. This class cannot be inherited.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)


public static class Timeout


A constant used to specify an infinite waiting period, for threading methods that accept an Int32 parameter.


A constant used to specify an infinite waiting period, for methods that accept a TimeSpan parameter.

The members of this class are used to specify infinite time-out intervals in threading operations. Infinite is used by methods that accept an integer millisecondsTimeout parameter, such as Thread.Sleep(Int32), Thread.Join(Int32), and ReaderWriterLock.AcquireReaderLock(Int32). InfiniteTimeSpan is used by methods that accept a timeout parameter of type TimeSpan, such as Thread.Sleep(TimeSpan), Thread.Join(TimeSpan), and ReaderWriterLock.AcquireReaderLock(TimeSpan).

The following example shows a thread going to sleep for an infinite time and subsequently being woken up.

using System;
using System.Security.Permissions;
using System.Threading;

class ThreadInterrupt
    static void Main()
        StayAwake stayAwake = new StayAwake();
        Thread newThread = 
            new Thread(new ThreadStart(stayAwake.ThreadMethod));

        // The following line causes an exception to be thrown 
        // in ThreadMethod if newThread is currently blocked
        // or becomes blocked in the future.
        Console.WriteLine("Main thread calls Interrupt on newThread.");

        // Tell newThread to go to sleep.
        stayAwake.SleepSwitch = true;

        // Wait for newThread to end.

class StayAwake
    bool sleepSwitch = false;

    public bool SleepSwitch
        set{ sleepSwitch = value; }

    public StayAwake(){}

    public void ThreadMethod()
        Console.WriteLine("newThread is executing ThreadMethod.");
            // Use SpinWait instead of Sleep to demonstrate the 
            // effect of calling Interrupt on a running thread.
            Console.WriteLine("newThread going to sleep.");

            // When newThread goes to sleep, it is immediately 
            // woken up by a ThreadInterruptedException.
        catch(ThreadInterruptedException e)
            Console.WriteLine("newThread cannot go to sleep - " +
                "interrupted by main thread.");

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

This type is thread safe.

Return to top
© 2015 Microsoft