Export (0) Print
Expand All

Timeout Class

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

System::Object
  System.Threading::Timeout

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

[ComVisibleAttribute(true)]
public ref class Timeout abstract sealed

The Timeout type exposes the following members.

  NameDescription
Top

  NameDescription
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryInfiniteA constant used to specify an infinite waiting period, for threading methods that accept an Int32 parameter.
Public fieldStatic memberInfiniteTimeSpanA constant used to specify an infinite waiting period, for methods that accept a TimeSpan parameter.
Top

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 namespace System;
using namespace System::Security::Permissions;
using namespace System::Threading;

ref class StayAwake
{
private:
   bool sleepSwitch;

public:

   property bool SleepSwitch 
   {
      void set( bool value )
      {
         sleepSwitch = value;
      }

   }
   StayAwake()
   {
      sleepSwitch = false;
   }

   void ThreadMethod()
   {
      Console::WriteLine( "newThread is executing ThreadMethod." );
      while (  !sleepSwitch )
      {

         // Use SpinWait instead of Sleep to demonstrate the  
         // effect of calling Interrupt on a running thread.
         Thread::SpinWait( 10000000 );
      }

      try
      {
         Console::WriteLine( "newThread going to sleep." );

         // When newThread goes to sleep, it is immediately  
         // woken up by a ThreadInterruptedException.
         Thread::Sleep( Timeout::Infinite );
      }
      catch ( ThreadInterruptedException^ /*e*/ ) 
      {
         Console::WriteLine( "newThread cannot go to sleep - " 
         "interrupted by main thread." );
      }

   }

};

int main()
{
   StayAwake^ stayAwake = gcnew StayAwake;
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( stayAwake, &StayAwake::ThreadMethod ) );
   newThread->Start();

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

   // Then tell newThread to go to sleep.
   stayAwake->SleepSwitch = true;

   // Wait for newThread to end.
   newThread->Join();
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

This type is thread safe.

Show:
© 2015 Microsoft