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)
{
}
}
}
文章来源于领测软件测试网 https://www.ltesting.net/