Leer en inglés

Compartir a través de


Timeout Clase

Definición

Contiene las constantes que especifican intervalos de tiempo de espera infinitos. Esta clase no puede heredarse.

public static class Timeout
public sealed class Timeout
[System.Runtime.InteropServices.ComVisible(true)]
public static class Timeout
Herencia
Timeout
Atributos

Ejemplos

En el ejemplo siguiente se muestra que un subproceso se va a suspender durante un tiempo infinito y, posteriormente, se reactiva.

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.");
        }
    }
}

Comentarios

Los miembros de esta clase se usan para especificar intervalos de tiempo de espera infinitos en las operaciones de subproceso. Infinite se usa mediante métodos que aceptan un parámetro entero millisecondsTimeout , como Thread.Sleep(Int32), Thread.Join(Int32)y ReaderWriterLock.AcquireReaderLock(Int32). InfiniteTimeSpan se usa mediante métodos que aceptan un timeout parámetro de tipo TimeSpan, como Thread.Sleep(TimeSpan), Thread.Join(TimeSpan)y ReaderWriterLock.AcquireReaderLock(TimeSpan).

Campos

Infinite

Una constante usada para especificar un período de espera infinito, para los métodos de subprocesamiento que aceptan un parámetro de Int32.

InfiniteTimeSpan

Constante usada para especificar un período de espera infinito, para los métodos que aceptan un parámetro de TimeSpan.

Se aplica a

Producto Versiones
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Seguridad para subprocesos

Este tipo es seguro para la ejecución de subprocesos.

Consulte también