可以设置全局变量bool tag如果收到报警就把tag = true主线程定时检查检查到变化后就处理,处理完了置tag= false注意线程间的同步。
.NET Framework 类库 Thread 类请参见
Thread 成员 | System.Threading 命名空间 | 线程与线程处理 | 使用线程和线程处理 | Thread 成员(Visual J# 语法) | C++ 托管扩展编程
要求
命名空间: System.Threading平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版 - Windows CE .NET程序集: Mscorlib (在 Mscorlib.dll 中)
语言
C#C++JScriptVisual Basic全部显示
创建并控制线程,设置其优先级并获取其状态。有关此类型所有成员的列表,请参阅 Thread 成员。System.Object
System.Threading.Thread[Visual Basic]
NotInheritable Public Class Thread[C#]
public sealed class Thread[C++]
public __gc __sealed class Thread[JScript]
public class Thread线程安全
该类型对于多线程操作是安全的。备注
一个进程可以创建一个或多个线程以执行与该进程关联的部分程序代码。使用 ThreadStart 委托指定由线程执行的程序代码。在线程存在期间,它总是处于由 ThreadState 定义的一个或多个状态中。可以为线程请求由 ThreadPriority 定义的调度优先级,但不能保证操作系统会接受该优先级。GetHashCode 提供托管线程的标识。在线程的生存期内,无论获取该值的应用程序域如何,它都不会和任何来自其他线程的值冲突。注意 操作系统 ThreadId 和托管线程没有固定关系,这是因为非托管宿主能控制托管与非托管线程之间的关系。特别是,复杂的宿主可以使用 CLR Hosting API 针对相同的操作系统线程调度很多托管线程,或者在不同的操作系统线程之间移动托管线程。
示例
[Visual Basic, C#, C++] 下面的代码示例说明简单的线程处理功能。[Visual Basic]
Imports System
Imports System.Threading' Simple threading scenario: Start a Shared method running
' on a second thread.
Public Class ThreadExample
' The ThreadProc method is called when the thread starts.
' It loops ten times, writing to the console and yielding
' the rest of its time slice each time, and then ends.
Public Shared Sub ThreadProc()
Dim i As Integer
For i = 0 To 9
Console.WriteLine("ThreadProc: {0}", i)
' Yield the rest of the time slice.
Thread.Sleep(0)
Next
End Sub Public Shared Sub Main()
Console.WriteLine("Main thread: Start a second thread.")
' The constructor for the Thread class requires a ThreadStart
' delegate. The Visual Basic AddressOf operator creates this
' delegate for you.
Dim t As New Thread(AddressOf ThreadProc)
' Start ThreadProc. On a uniprocessor, the thread does not get
' any processor time until the main thread yields. Uncomment
' the Thread.Sleep that follows t.Start() to see the difference.
t.Start()
'Thread.Sleep(0) Dim i As Integer
For i = 1 To 4
Console.WriteLine("Main thread: Do some work.")
Thread.Sleep(0)
Next Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.")
t.Join()
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.")
Console.ReadLine()
End Sub
End Class[C#]
using System;
using System.Threading;// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample {
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static void ThreadProc() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
} public static void Main() {
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
// Start ThreadProc. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0); for (int i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
} Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}[C++] // [C++]
// Compile using /clr option.
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
// Simple threading scenario: Start a Shared method running
// on a second thread.
public __gc class ThreadExample
{
public:
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
static void ThreadProc()
{
for (int i = 0; i < 10; i++)
{
Console::Write("ThreadProc: ");
Console::WriteLine(i);
// Yield the rest of the time slice.
Thread::Sleep(0);
}
}
};
int main()
{
Console::WriteLine(S"Main thread: Start a second thread.");
// Create the thread, passing a ThreadStart delegate that
// represents the ThreadExample::ThreadProc method. For a
// delegate representing a static method, no object is
// required.
Thread *oThread = new Thread(new ThreadStart(0, &ThreadExample::ThreadProc));
// Start the thread. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
oThread->Start();
//Thread::Sleep(0); for (int i = 0; i < 4; i++) {
Console::WriteLine("Main thread: Do some work.");
Thread::Sleep(0);
} Console::WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
oThread->Join();
Console::WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console::ReadLine();
return 0;
}[Visual Basic, C#, C++] 此代码产生的输出类似如下内容:[VB, C++, C#]
Main thread: Start a second thread.
Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
Main thread: Call Join(), to wait until ThreadProc ends.
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
Main thread: ThreadProc.Join has returned. Press Enter to end program.
.NET Framework 类库 Thread 类请参见
Thread 成员 | System.Threading 命名空间 | 线程与线程处理 | 使用线程和线程处理 | Thread 成员(Visual J# 语法) | C++ 托管扩展编程
要求
命名空间: System.Threading平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版 - Windows CE .NET程序集: Mscorlib (在 Mscorlib.dll 中)
语言
C#C++JScriptVisual Basic全部显示
创建并控制线程,设置其优先级并获取其状态。有关此类型所有成员的列表,请参阅 Thread 成员。System.Object
System.Threading.Thread[Visual Basic]
NotInheritable Public Class Thread[C#]
public sealed class Thread[C++]
public __gc __sealed class Thread[JScript]
public class Thread线程安全
该类型对于多线程操作是安全的。备注
一个进程可以创建一个或多个线程以执行与该进程关联的部分程序代码。使用 ThreadStart 委托指定由线程执行的程序代码。在线程存在期间,它总是处于由 ThreadState 定义的一个或多个状态中。可以为线程请求由 ThreadPriority 定义的调度优先级,但不能保证操作系统会接受该优先级。GetHashCode 提供托管线程的标识。在线程的生存期内,无论获取该值的应用程序域如何,它都不会和任何来自其他线程的值冲突。注意 操作系统 ThreadId 和托管线程没有固定关系,这是因为非托管宿主能控制托管与非托管线程之间的关系。特别是,复杂的宿主可以使用 CLR Hosting API 针对相同的操作系统线程调度很多托管线程,或者在不同的操作系统线程之间移动托管线程。
示例
[Visual Basic, C#, C++] 下面的代码示例说明简单的线程处理功能。[Visual Basic]
Imports System
Imports System.Threading' Simple threading scenario: Start a Shared method running
' on a second thread.
Public Class ThreadExample
' The ThreadProc method is called when the thread starts.
' It loops ten times, writing to the console and yielding
' the rest of its time slice each time, and then ends.
Public Shared Sub ThreadProc()
Dim i As Integer
For i = 0 To 9
Console.WriteLine("ThreadProc: {0}", i)
' Yield the rest of the time slice.
Thread.Sleep(0)
Next
End Sub Public Shared Sub Main()
Console.WriteLine("Main thread: Start a second thread.")
' The constructor for the Thread class requires a ThreadStart
' delegate. The Visual Basic AddressOf operator creates this
' delegate for you.
Dim t As New Thread(AddressOf ThreadProc)
' Start ThreadProc. On a uniprocessor, the thread does not get
' any processor time until the main thread yields. Uncomment
' the Thread.Sleep that follows t.Start() to see the difference.
t.Start()
'Thread.Sleep(0) Dim i As Integer
For i = 1 To 4
Console.WriteLine("Main thread: Do some work.")
Thread.Sleep(0)
Next Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.")
t.Join()
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.")
Console.ReadLine()
End Sub
End Class[C#]
using System;
using System.Threading;// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample {
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static void ThreadProc() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
} public static void Main() {
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
// Start ThreadProc. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0); for (int i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
} Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}[C++] // [C++]
// Compile using /clr option.
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
// Simple threading scenario: Start a Shared method running
// on a second thread.
public __gc class ThreadExample
{
public:
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
static void ThreadProc()
{
for (int i = 0; i < 10; i++)
{
Console::Write("ThreadProc: ");
Console::WriteLine(i);
// Yield the rest of the time slice.
Thread::Sleep(0);
}
}
};
int main()
{
Console::WriteLine(S"Main thread: Start a second thread.");
// Create the thread, passing a ThreadStart delegate that
// represents the ThreadExample::ThreadProc method. For a
// delegate representing a static method, no object is
// required.
Thread *oThread = new Thread(new ThreadStart(0, &ThreadExample::ThreadProc));
// Start the thread. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
oThread->Start();
//Thread::Sleep(0); for (int i = 0; i < 4; i++) {
Console::WriteLine("Main thread: Do some work.");
Thread::Sleep(0);
} Console::WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
oThread->Join();
Console::WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console::ReadLine();
return 0;
}[Visual Basic, C#, C++] 此代码产生的输出类似如下内容:[VB, C++, C#]
Main thread: Start a second thread.
Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
Main thread: Call Join(), to wait until ThreadProc ends.
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
Main thread: ThreadProc.Join has returned. Press Enter to end program.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货