[原创]c#中使用多线程(图)二[3]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

using System;
using System.Collections;
using System.Threading;

namespace student
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 public class Class1
 {

  public ArrayList Threads = new ArrayList();
  public ArrayList Result = new ArrayList();

  public event EventHandler Update;
  public delegate void EventHandler(int index);
  
  public Class1()
  {
   //
   // TODO: http://blog.csdn.net/iuhxq
   //
   Update += new EventHandler(Class1_Update);
  }

  private void Process()
  {
   int i = 1;
   while(true)
   {
    i++;
    int index = Threads.IndexOf(Thread.CurrentThread);
    Result[index] = i;
    if(Update!=null)Update(index);
    Thread.Sleep(0);
   }
  }

  public int Add()
  {
   Thread t = new Thread(new ThreadStart(Process));
   t.Start();
   Threads.Add(t);
   lock(Result)
   {
    Result.Add(0);
   }
   return Threads.Count-1;
  }

  public int Del()
  {
   int count = Threads.Count;
   if(count>0)
   {
    Thread t1 = (Thread)Threads[count-1];
    if(t1.IsAlive)
    {
     t1.Abort();
    }
    Threads.RemoveAt(count-1);
    lock(Result)
    {
     Result.RemoveAt(count-1);
    }
   }
   return count-1;
  }

  private void Class1_Update(int index)
  {

  }
 }
}

本文关键:[原创]c#中使用多线程(图)二
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top