C# 多线程操作样例_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C# 多线程操作样例

C# 多线程操作样例

 2015/4/29 11:47:36  电子灵魂  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Threading;//引用多线程namespaceThreadTest{publicclassAlpha{publicvoidBeta(){while(true){Console.WriteLine("Alpha.Betaisrunninginitsownthread.");}}};publicclassSimple{publicstaticintMain(){Console.WriteLine
  • 标签:C# 多线程 操作 线程

 

using System;
using System.Threading; //引用多线程

namespace ThreadTest
{
    public class Alpha
    {
        public void Beta()
        {
            while (true)
            {
                Console.WriteLine("Alpha.Beta is running in its own thread.");
            }
        }
    };

    public class Simple
    {
        public static int Main()
        {
            Console.WriteLine("Thread Start/Stop/Join Sample");
            Alpha oAlpha = new Alpha();
        //file://这里创建一个线程,使之执行Alpha类的Beta()方法
            Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
            oThread.Start();
            while (!oThread.IsAlive)
                Thread.Sleep(1);
            oThread.Abort();
            oThread.Join();
            Console.WriteLine();
            Console.WriteLine("Alpha.Beta has finished");
            try
            {
                Console.WriteLine("Try to restart the Alpha.Beta thread");
                oThread.Start();
            }
            catch (ThreadStateException)
            {
                Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
                Console.WriteLine("Expected since aborted threads cannot be restarted.");
                Console.ReadLine();
            }
            return 0;
        }
    }
}
上一篇: ComponentOne Studio for Enterprise 2015 v1 全新发布 下一篇: 没有下一篇了!
发表评论
用户名: 匿名