ThreadInterruptedException Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Pengecualian yang dilemparkan ketika Thread terganggu saat dalam keadaan menunggu.
public ref class ThreadInterruptedException : SystemException
[System.Serializable]
public class ThreadInterruptedException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ThreadInterruptedException : SystemException
public class ThreadInterruptedException : SystemException
[<System.Serializable>]
type ThreadInterruptedException = class
inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ThreadInterruptedException = class
inherit SystemException
type ThreadInterruptedException = class
inherit SystemException
Public Class ThreadInterruptedException
Inherits SystemException
- Warisan
- Atribut
Contoh
Contoh kode berikut menunjukkan perilaku utas yang sedang berjalan saat terganggu dan kemudian diblokir.
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
Keterangan
Setelah utas dibuat, utas berada dalam satu atau beberapa ThreadState status sampai dihancurkan. Memanggil Interrupt ketika utas dalam WaitSleepJoin status akan menyebabkan ThreadInterruptedException dilemparkan ke utas target. Jika utas tidak dalam WaitSleepJoin status, pengecualian tidak dilemparkan sampai utas memasuki status tersebut. Jika utas tidak pernah diblokir, itu bisa selesai tanpa pernah diputus.
ThreadInterruptedException menggunakan COR_E_THREADINTERRUPTED HRESULT, yang memiliki nilai 0x80131519.
Untuk daftar nilai properti awal untuk instance ThreadInterruptedException, silakan lihat konstruktor ThreadInterruptedException.
Konstruktor
| Nama | Deskripsi |
|---|---|
| ThreadInterruptedException() |
Menginisialisasi instans ThreadInterruptedException baru kelas dengan properti default. |
| ThreadInterruptedException(SerializationInfo, StreamingContext) |
Menginisialisasi instans ThreadInterruptedException baru kelas dengan data berseri. |
| ThreadInterruptedException(String, Exception) |
Menginisialisasi instans ThreadInterruptedException baru kelas dengan pesan kesalahan tertentu dan referensi ke pengecualian dalam yang merupakan penyebab pengecualian ini. |
| ThreadInterruptedException(String) |
Menginisialisasi instans baru kelas ThreadInterruptedException dengan pesan kesalahan tertentu. |
Properti
| Nama | Deskripsi |
|---|---|
| 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 |
Mendapatkan atau mengatur 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
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
| GetBaseException() |
Ketika ditimpa di kelas turunan, mengembalikan Exception 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, mengatur SerializationInfo dengan informasi tentang pengecualian. (Diperoleh dari Exception) |
| GetType() |
Mendapatkan jenis runtime instans saat ini. (Diperoleh dari Exception) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Membuat dan mengembalikan representasi string dari pengecualian saat ini. (Diperoleh dari Exception) |
Acara
| Nama | Deskripsi |
|---|---|
| SerializeObjectState |
Terjadi ketika pengecualian diserialisasikan untuk membuat objek status pengecualian yang berisi data berseri tentang pengecualian. (Diperoleh dari Exception) |