木子屋 Dnawo's BLOG

C#获取线程编号示例

👤 dnawo 📅 2014-08-29 👁 4035 👍 0 💬 0 🔄 来源:本站原创
有时我们需要确定应用程序是否启用了多线程,此时可以通过获取线程编号进行判断(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)

暂无评论。

验证码
评论需审核通过后显示
← 上一篇 阿里云5周年亿万回馈 下一篇 → 闽运网上售票网站订票流程