C#获取线程编号示例

有时我们需要确定应用程序是否启用了多线程,此时可以通过获取线程编号进行判断(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);
        }
    }
}



上一篇: 阿里云5周年亿万回馈
下一篇: 闽运网上售票网站订票流程
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 3086
发表评论
登录后再发表评论!