Timeout 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
包含指定无限期超时间隔的常数。 此类不能被继承。
public ref class Timeout abstract sealedpublic ref class Timeout sealedpublic static class Timeoutpublic sealed class Timeout[System.Runtime.InteropServices.ComVisible(true)]
public static class Timeouttype Timeout = class[<System.Runtime.InteropServices.ComVisible(true)>]
type Timeout = classPublic Class TimeoutPublic NotInheritable Class Timeout- 继承
- 
				Timeout
- 属性
示例
以下示例显示了一个线程无限期进入睡眠状态,随后被唤醒。
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));
        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.");
        // Tell newThread to go to sleep.
        stayAwake.SleepSwitch = true;
        // Wait for newThread to end.
        newThread.Join();
    }
}
class StayAwake
{
    bool sleepSwitch = false;
    public bool SleepSwitch
    {
        set{ sleepSwitch = value; }
    }
    public StayAwake(){}
    public 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.");
        }
    }
}
Option Explicit
Option Strict
Imports System.Security.Permissions
Imports System.Threading
Public Class ThreadInterrupt
    <MTAThread> _
    Shared Sub Main()
        Dim stayAwake As New StayAwake()
        Dim newThread As New Thread(AddressOf 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.")
        ' Tell newThread to go to sleep.
        stayAwake.SleepSwitch = True
        ' Wait for newThread to end.
        newThread.Join()
    End Sub
End Class
Public Class StayAwake
    Dim sleepSwitchValue As Boolean = False
    WriteOnly Property SleepSwitch As Boolean
        Set
            sleepSwitchValue = Value
        End Set
    End Property 
    Sub New()
    End Sub
    Sub ThreadMethod()
        Console.WriteLine("newThread is executing ThreadMethod.")
        While Not sleepSwitchValue
            ' Use SpinWait instead of Sleep to demonstrate the 
            ' effect of calling Interrupt on a running thread.
            Thread.SpinWait(10000000)
        End While
        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 ex As ThreadInterruptedException
            Console.WriteLine("newThread cannot go to " & _
                "sleep - interrupted by main thread.")
        End Try
    End Sub
End Class
注解
此类的成员用于指定线程操作中的无限超时间隔。 
              Infinite 由接受整数 millisecondsTimeout 参数的方法(如 Thread.Sleep(Int32)、 Thread.Join(Int32)和 ReaderWriterLock.AcquireReaderLock(Int32))使用。 
              InfiniteTimeSpan 由接受 timeout 类型 TimeSpan参数的方法使用,例如 Thread.Sleep(TimeSpan)、 Thread.Join(TimeSpan)和 ReaderWriterLock.AcquireReaderLock(TimeSpan)。
字段
| Infinite | 一个用于指定无限长等待时间的常数,适用于接受 Int32 参数的线程处理方法。 | 
| InfiniteTimeSpan | 用于指定无限长等待时间的常数,接受 TimeSpan 参数的方法。 | 
适用于
线程安全性
此类型是线程安全的。