有时我们需要确定应用程序是否启用了多线程,此时可以通过获取线程编号进行判断(Thread.Name属性值常为空不能用于判断):
代码如下 |
复制代码 |
using System;
using System.Threading;
namespace ConsoleApplication1
{
public class Program
{
static public void Main(string[] args)
{
Console.WriteLine("MainThreadId:" + Thread.CurrentThread.ManagedThreadId);
Action test = Test;
test.BeginInvoke("mzwu.com", TestCallback, test);
Console.ReadKey();
}
static void Test(string str)
{
Console.WriteLine("SubThreadId:" + Thread.CurrentThread.ManagedThreadId);
}
static void TestCallback(IAsyncResult ar)
{
Action handler = (Action)ar.AsyncState;
handler.EndInvoke(ar);
}
}
}
|