TimeoutException Kelas

Definisi

Pengecualian yang dilemparkan ketika waktu yang dialokasikan untuk proses atau operasi telah kedaluwarsa.

public ref class TimeoutException : Exception
public ref class TimeoutException : SystemException
public class TimeoutException : Exception
public class TimeoutException : SystemException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class TimeoutException : SystemException
type TimeoutException = class
    inherit Exception
type TimeoutException = class
    inherit SystemException
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type TimeoutException = class
    inherit SystemException
Public Class TimeoutException
Inherits Exception
Public Class TimeoutException
Inherits SystemException
Warisan
TimeoutException
Warisan
TimeoutException
Turunan
Atribut

Contoh

Contoh kode berikut menunjukkan penggunaan TimeoutException bersama dengan anggota System.IO.Ports.SerialPort kelas.

// This example demonstrates the use of the TimeoutException
// exception in conjunction with the SerialPort class.

#using <System.dll>

using namespace System;
using namespace System::IO::Ports;

int main()
{
    String^ input;
    try
    {
        // Set the COM1 serial port to speed = 4800 baud, parity = odd,
        // data bits = 8, stop bits = 1.
        SerialPort^ port = gcnew SerialPort("COM1",
            4800, Parity::Odd, 8, StopBits::One);
        // Timeout after 2 seconds.
        port->ReadTimeout = 2000;
        port->Open();

        // Read until either the default newline termination string
        // is detected or the read operation times out.
        input = port->ReadLine();

        port->Close();

        // Echo the input.
        Console::WriteLine(input);
    }

    // Only catch timeout exceptions.
    catch (TimeoutException^ ex)
    {
        Console::WriteLine(ex);
    }
};
/*
This example produces the following results:

(Data received at the serial port is echoed to the console if the
read operation completes successfully before the specified timeout period
expires. Otherwise, a timeout exception like the following is thrown.)

System.TimeoutException: The operation has timed-out.
at System.IO.Ports.SerialStream.ReadByte(Int32 timeout)
at System.IO.Ports.SerialPort.ReadOneChar(Int32 timeout)
at System.IO.Ports.SerialPort.ReadTo(String value)
at System.IO.Ports.SerialPort.ReadLine()
at Sample.Main()
*/
// This example demonstrates the use of the TimeoutException
// exception in conjunction with the SerialPort class.

using System;
using System.IO.Ports;

class Sample
{
    public static void Main()
    {
    string input;
    try
        {
// Set the COM1 serial port to speed = 4800 baud, parity = odd,
// data bits = 8, stop bits = 1.
        SerialPort sp = new SerialPort("COM1",
                        4800, Parity.Odd, 8, StopBits.One);
// Timeout after 2 seconds.
        sp.ReadTimeout = 2000;
        sp.Open();

// Read until either the default newline termination string
// is detected or the read operation times out.
        input = sp.ReadLine();

        sp.Close();

// Echo the input.
        Console.WriteLine(input);
        }

// Only catch timeout exceptions.
    catch (TimeoutException e)
        {
        Console.WriteLine(e);
        }
    }
}
/*
This example produces the following results:

(Data received at the serial port is echoed to the console if the
read operation completes successfully before the specified timeout period
expires. Otherwise, a timeout exception like the following is thrown.)

System.TimeoutException: The operation has timed-out.
   at System.IO.Ports.SerialStream.ReadByte(Int32 timeout)
   at System.IO.Ports.SerialPort.ReadOneChar(Int32 timeout)
   at System.IO.Ports.SerialPort.ReadTo(String value)
   at System.IO.Ports.SerialPort.ReadLine()
   at Sample.Main()
*/
// This example demonstrates the use of the TimeoutException
// exception in conjunction with the SerialPort class.

open System
open System.IO.Ports

try
// Set the COM1 serial port to speed = 4800 baud, parity = odd,
// data bits = 8, stop bits = 1.
    let sp = new SerialPort("COM1", 4800, Parity.Odd, 8, StopBits.One)
// Timeout after 2 seconds.
    sp.ReadTimeout <- 2000
    sp.Open()

// Read until either the default newline termination string
// is detected or the read operation times out.
    let input = sp.ReadLine()

    sp.Close()

// Echo the input.
    printfn $"{input}"

// Only catch timeout exceptions.
with :? TimeoutException as e ->
    printfn $"{e}"
(*
This example produces the following results:

(Data received at the serial port is echoed to the console if the
read operation completes successfully before the specified timeout period
expires. Otherwise, a timeout exception like the following is thrown.)

System.TimeoutException: The operation has timed-out.
   at System.IO.Ports.SerialStream.ReadByte(Int32 timeout)
   at System.IO.Ports.SerialPort.ReadOneChar(Int32 timeout)
   at System.IO.Ports.SerialPort.ReadTo(String value)
   at System.IO.Ports.SerialPort.ReadLine()
   at Sample.main()
*)
' This example demonstrates the use of the TimeoutException 
' exception in conjunction with the SerialPort class.

Imports System.IO.Ports

Class Sample
   Public Shared Sub Main()
      Dim input As String
      Try
         ' Set the COM1 serial port to speed = 4800 baud, parity = odd, 
         ' data bits = 8, stop bits = 1.
         Dim sp As New SerialPort("COM1", 4800, Parity.Odd, 8, StopBits.One)

         ' Timeout after 2 seconds.
         sp.ReadTimeout = 2000
         sp.Open()
         
         ' Read until either the default newline termination string 
         ' is detected or the read operation times out.
         input = sp.ReadLine()

         sp.Close()

         ' Echo the input.
         Console.WriteLine(input)
      
      ' Only catch timeout exceptions.
      Catch e As TimeoutException
         Console.WriteLine(e)
      End Try
   End Sub
End Class
'
'This example produces the following results:
'
'(Data received at the serial port is echoed to the console if the 
'read operation completes successfully before the specified timeout period 
'expires. Otherwise, a timeout exception like the following is thrown.)
'
'System.TimeoutException: The operation has timed-out.
'   at System.IO.Ports.SerialStream.ReadByte(Int32 timeout)
'   at System.IO.Ports.SerialPort.ReadOneChar(Int32 timeout)
'   at System.IO.Ports.SerialPort.ReadTo(String value)
'   at System.IO.Ports.SerialPort.ReadLine()
'   at Sample.Main()
'

Keterangan

Untuk informasi tentang mengatur interval waktu habis sebelum TimeoutException pengecualian dilemparkan, lihat dokumentasi untuk jenis yang waktu metodenya habis. Untuk informasi tentang menangani waktu habis, lihat dokumentasi untuk metode yang melemparkan pengecualian.

TimeoutException menggunakan HRESULT, COR_E_TIMEOUT, yang memiliki nilai 0x80131505.

Untuk daftar nilai properti awal untuk instans TimeoutException, lihat TimeoutException konstruktor.

Konstruktor

TimeoutException()

Menginisialisasi instans baru kelas TimeoutException.

TimeoutException(SerializationInfo, StreamingContext)

Menginisialisasi instans TimeoutException baru kelas dengan data berseri.

TimeoutException(String)

Menginisialisasi instans TimeoutException baru kelas dengan pesan kesalahan yang ditentukan.

TimeoutException(String, Exception)

Menginisialisasi instans TimeoutException baru kelas dengan pesan kesalahan dan pengecualian dalam yang ditentukan.

Properti

Data

Mendapatkan kumpulan pasangan kunci/nilai yang memberikan informasi tambahan yang ditentukan pengguna tentang pengecualian.

(Diperoleh dari Exception)
HelpLink

Mendapatkan atau mengatur tautan ke file bantuan yang terkait dengan pengecualian ini.

(Diperoleh dari Exception)
HResult

Mendapatkan atau mengatur HRESULT, nilai numerik berkode yang ditetapkan ke pengecualian tertentu.

(Diperoleh dari Exception)
InnerException

Mendapatkan instans Exception yang menyebabkan pengecualian saat ini.

(Diperoleh dari Exception)
Message

Mendapatkan pesan yang menjelaskan pengecualian saat ini.

(Diperoleh dari Exception)
Source

Get dan set nama aplikasi atau objek yang menyebabkan kesalahan.

(Diperoleh dari Exception)
StackTrace

Mendapatkan representasi string dari bingkai langsung pada tumpukan panggilan.

(Diperoleh dari Exception)
TargetSite

Mendapatkan metode yang melemparkan pengecualian saat ini.

(Diperoleh dari Exception)

Metode

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetBaseException()

Ketika ditimpa di kelas turunan Exception , mengembalikan yang merupakan akar penyebab dari satu atau beberapa pengecualian berikutnya.

(Diperoleh dari Exception)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetObjectData(SerializationInfo, StreamingContext)

Saat ditimpa di kelas turunan SerializationInfo , mengatur dengan informasi tentang pengecualian.

(Diperoleh dari Exception)
GetType()

Mendapatkan jenis runtime dari instans saat ini.

(Diperoleh dari Exception)
MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(Diperoleh dari Object)
ToString()

Membuat dan mengembalikan representasi string dari pengecualian saat ini.

(Diperoleh dari Exception)

Acara

SerializeObjectState
Kedaluwarsa.

Terjadi ketika pengecualian diserialisasikan untuk membuat objek status pengecualian yang berisi data berseri tentang pengecualian.

(Diperoleh dari Exception)

Berlaku untuk

Lihat juga