有时我们需要确定应用程序是否启用了多线程,此时可以通过获取线程编号进行判断(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<string> 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<string> handler = (Action<string>)ar.AsyncState;
handler.EndInvoke(ar);
}
}
}
评论(0)
暂无评论。