ThreadStateException 类
当 Thread 处于对方法调用无效的 ThreadState 时引发的异常。
**命名空间:**System.Threading
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class ThreadStateException
Inherits SystemException
用法
Dim instance As ThreadStateException
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class ThreadStateException : SystemException
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class ThreadStateException : public SystemException
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public class ThreadStateException extends SystemException
SerializableAttribute
ComVisibleAttribute(true)
public class ThreadStateException extends SystemException
备注
一旦创建线程,它就至少处于 ThreadState 中的一个状态中,直到终止。由于线程的当前状态而无法执行所请求的操作的方法将引发 ThreadStateException。例如,试图通过对已终止的线程调用 Start 来重新启动已中止的线程将引发 ThreadStateException。
ThreadStateException 使用值为 0x80131520 的 HRESULT COR_E_THREADSTATE。
有关 ThreadStateException 实例的初始属性值列表,请参见 ThreadStateException 构造函数。
示例
下面的示例说明导致系统引发 ThreadStateException 的错误。
Imports System
Imports System.Threading
Public Class ThreadWork
Public Shared Sub DoWork()
Console.WriteLine("Working thread...")
End Sub 'DoWork
End Class 'ThreadWork
Class ThreadStateTest
Public Shared Sub Main()
Dim myThreadDelegate As New ThreadStart(AddressOf ThreadWork.DoWork)
Dim myThread As New Thread(myThreadDelegate)
myThread.Start()
Thread.Sleep(0)
Console.WriteLine("In main. Attempting to restart myThread.")
Try
myThread.Start()
Catch e As ThreadStateException
Console.WriteLine("Caught: {0}", e.Message)
End Try
End Sub 'Main
End Class 'ThreadStateTest
using System;
using System.Threading;
public class ThreadWork
{
public static void DoWork()
{
Console.WriteLine("Working thread...");
}
}
class ThreadStateTest
{
public static void Main()
{
ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
Thread.Sleep(0);
Console.WriteLine("In main. Attempting to restart myThread.");
try
{
myThread.Start();
}
catch(ThreadStateException e)
{
Console.WriteLine("Caught: {0}", e.Message);
}
}
}
using namespace System;
using namespace System::Threading;
ref class ThreadWork
{
public:
static void DoWork()
{
Console::WriteLine( "Working thread..." );
}
};
int main()
{
ThreadStart^ myThreadDelegate = gcnew ThreadStart( ThreadWork::DoWork );
Thread^ myThread = gcnew Thread( myThreadDelegate );
myThread->Start();
Thread::Sleep( 0 );
Console::WriteLine( "In main. Attempting to restart myThread." );
try
{
myThread->Start();
}
catch ( ThreadStateException^ e )
{
Console::WriteLine( "Caught: {0}", e->Message );
}
}
import System.*;
import System.Threading.*;
public class ThreadWork
{
public static void DoWork()
{
Console.WriteLine("Working thread...");
} //DoWork
} //ThreadWork
class ThreadStateTest
{
public static void main(String[] args)
{
ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
System.Threading.Thread myThread =
new System.Threading.Thread(myThreadDelegate);
myThread.Start();
System.Threading.Thread.Sleep(0);
Console.WriteLine("In main. Attempting to restart myThread.");
try {
myThread.Start();
}
catch (ThreadStateException e) {
Console.WriteLine("Caught: {0}", e.get_Message());
}
} //main
} //ThreadStateTest
这段代码产生以下输出:
In main. Attempting to restart myThread.
Working thread...
Caught: Thread is running or terminated. Cannot restart.
继承层次结构
System.Object
System.Exception
System.SystemException
System.Threading.ThreadStateException
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
ThreadStateException 成员
System.Threading 命名空间
Thread 类
ThreadState 枚举
Exception