C#多线程操作

static void Main(string[] args)
        {

            Thread thread = Thread.CurrentThread;
            thread.Name = "Main Thread";
            string threadMessage = string.Format("Thread ID:{0}\n    Current AppDomainId:{1}\n    " +
                "Current ContextId:{2}\n    Thread Name:{3}\n    " +
                "Thread State:{4}\n    Thread Priority:{5}\n",
                thread.ManagedThreadId, Thread.GetDomainID(), Thread.CurrentContext.ContextID,
                thread.Name, thread.ThreadState, thread.Priority);

            Console.WriteLine(threadMessage);
            Console.WriteLine("Main threadId is:" + Thread.CurrentThread.ManagedThreadId);
            Message message = new Message();
            Thread thread1 = new Thread(new ThreadStart(message.ShowMessage));
            thread1.Start();
            Console.WriteLine("Do something ..........!");
            Console.WriteLine("Main thread working is complete!");


            Console.ReadKey();
        }

新建Message类:

class Message
    {
        public void ShowMessage()
        {
            string message = string.Format("Async threadId is :{0}",Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine(message);

            for (int n = 0; n < 10; n++)
            {
                Thread.Sleep(300);
                Console.WriteLine("The number is:" + n.ToString());
            }
        }
    }

结果为:

 

posted @ 2017-11-24 11:41  凉城旧巷旧少年  阅读(182)  评论(0编辑  收藏  举报