现在的位置: 首页 > 综合 > 正文

C#动态多线程创建(勇哥讲解)

2012年10月07日 ⁄ 综合 ⁄ 共 873字 ⁄ 字号 评论关闭

using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Threading;

namespace Test
{
class Program
{
  class ThreadProc
  {
    int _i = 0;

    public ThreadProc(int i)
    {
      this._i = i;
    }

    public void thread_proc()
    {
      while (true)
      {
        Console.WriteLine("This is thread {0}. Current Time is {1}.",
        _i, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));

        Thread.Sleep(10000);
      }
    }
  }

    static void Main(string[] args)
    {
      int MAX_THREADS = 70;
      ThreadProc[] tproc = new ThreadProc[MAX_THREADS];
      Thread[] oThread = new Thread[MAX_THREADS];

      for (int i = 0; i < MAX_THREADS; i++)
      {
        tproc[i] = new ThreadProc(i);
        oThread[i] = new Thread(new ThreadStart(tproc[i].thread_proc));
        oThread[i].Start();
      }

      Thread.Sleep(300000);

      for (int i = 0; i < MAX_THREADS; i++)
      {
        oThread[i].Abort();
      }
    }
  }
}

抱歉!评论已关闭.