先写一个用于线程的类

1
public
class
work
2 {
3 // 定义一个线程
4 Thread thread = null ;
5 // 关键字
6 string Key = string .Empty;
7 // 控制while语句的
8 bool s = false ;
9 string value = string .Empty;
10 int i = 0 ;
11 // 构造方法
12 public work( string name)
13 {
14 this .Key = name;
15 }
16 // 开启线程,实例化线程
17 public void start()
18 {
19 thread = new Thread(read);
20 thread.Name = Key;
21 thread.Start();
22 }
23 // 线程开启要执行的方法
24 public void read()
25 {
26 while ( ! s)
27 {
28 value = thread.Name + " 线程第 " + i + " 次执行 " ;
29 Thread.Sleep( 2000 );
30 i ++ ;
31 // 在这里把你想要处理的方法放进来
32 }
33 }
34 // 停止线程
35 public void stop() // 线程停止
36 {
37 s = true ;
38 this .thread.Abort();
39 }
40 // 获取线程状态
41 public string GetThreadState()
42 {
43 return thread.ThreadState.ToString();
44 }
45 // 获取线程第几次执行
46 public string GetValue()
47 {
48 return value;
49 }
50 }
51
2 {
3 // 定义一个线程
4 Thread thread = null ;
5 // 关键字
6 string Key = string .Empty;
7 // 控制while语句的
8 bool s = false ;
9 string value = string .Empty;
10 int i = 0 ;
11 // 构造方法
12 public work( string name)
13 {
14 this .Key = name;
15 }
16 // 开启线程,实例化线程
17 public void start()
18 {
19 thread = new Thread(read);
20 thread.Name = Key;
21 thread.Start();
22 }
23 // 线程开启要执行的方法
24 public void read()
25 {
26 while ( ! s)
27 {
28 value = thread.Name + " 线程第 " + i + " 次执行 " ;
29 Thread.Sleep( 2000 );
30 i ++ ;
31 // 在这里把你想要处理的方法放进来
32 }
33 }
34 // 停止线程
35 public void stop() // 线程停止
36 {
37 s = true ;
38 this .thread.Abort();
39 }
40 // 获取线程状态
41 public string GetThreadState()
42 {
43 return thread.ThreadState.ToString();
44 }
45 // 获取线程第几次执行
46 public string GetValue()
47 {
48 return value;
49 }
50 }
51
再写用户开启多线程跟关闭,管理的具体方法

1
protected
void
open_Click(
object
sender, EventArgs e)
2 {
3 // 定义一个哈希表
4 Hashtable ht = new Hashtable();
5 // session是要存哈希用的
6 if (Session[ " thread " ] != null )
7 {
8 try
9 {
10 // 实例化并开启线程
11 work w = null ;
12 if (tb.Text.Trim() != "" )
13 w = new work(tb.Text.Trim());
14 w.start();
15
16 // 将已经存入session中的哈希信息传给ht
17 ht = (Hashtable)Session[ " thread " ];
18 // 将线程存入哈希表中
19 ht.Add(tb.Text.Trim(), w);
20 // 打印线程信息
21 PrintThread();
22 HttpContext.Current.Response.Write( " <script>alert('线程创建成功');</script> " );
23 }
24 catch (Exception ex)
25 {
26 HttpContext.Current.Response.Write( " <script>alert('来自创建有session: " + ex.Message + " ');</script> " );
27 }
28 }
29 else
30 {
31 try
32 {
33 work w = null ;
34 if (tb.Text.Trim() != "" )
35 w = new work(tb.Text.Trim());
36 w.start();
37
38 ht.Add(tb.Text.Trim(), w);
39 Session[ " thread " ] = ht;
40 PrintThread();
41 }
42 catch (Exception ex)
43 {
44 HttpContext.Current.Response.Write( " <script>alert('来自创建无session " + ex.Message + " ');</script> " );
45 }
46 }
47 }
2 {
3 // 定义一个哈希表
4 Hashtable ht = new Hashtable();
5 // session是要存哈希用的
6 if (Session[ " thread " ] != null )
7 {
8 try
9 {
10 // 实例化并开启线程
11 work w = null ;
12 if (tb.Text.Trim() != "" )
13 w = new work(tb.Text.Trim());
14 w.start();
15
16 // 将已经存入session中的哈希信息传给ht
17 ht = (Hashtable)Session[ " thread " ];
18 // 将线程存入哈希表中
19 ht.Add(tb.Text.Trim(), w);
20 // 打印线程信息
21 PrintThread();
22 HttpContext.Current.Response.Write( " <script>alert('线程创建成功');</script> " );
23 }
24 catch (Exception ex)
25 {
26 HttpContext.Current.Response.Write( " <script>alert('来自创建有session: " + ex.Message + " ');</script> " );
27 }
28 }
29 else
30 {
31 try
32 {
33 work w = null ;
34 if (tb.Text.Trim() != "" )
35 w = new work(tb.Text.Trim());
36 w.start();
37
38 ht.Add(tb.Text.Trim(), w);
39 Session[ " thread " ] = ht;
40 PrintThread();
41 }
42 catch (Exception ex)
43 {
44 HttpContext.Current.Response.Write( " <script>alert('来自创建无session " + ex.Message + " ');</script> " );
45 }
46 }
47 }
关闭线程

1
//
关闭线程
2 protected void close_Click( object sender, EventArgs e)
3 {
4 if (Session[ " thread " ] == null )
5 HttpContext.Current.Response.Write( " <script>alert('对不起,线程为空');</script> " );
6 else
7 {
8 try
9 {
10 // 将线程哈希取出
11 Hashtable ht = (Hashtable)Session[ " thread " ];
12 // 停止线程
13 work w = (work)ht[tb.Text.Trim()];
14 w.stop();
15 w = null ;
16 ht.Remove(tb.Text.Trim());
17 // 打印信息
18 PrintThread();
19 HttpContext.Current.Response.Write( " <script>alert('线程 " + tb.Text.Trim() + " 已关闭');</script> " );
20 }
21 catch (Exception ex)
22 {
23 HttpContext.Current.Response.Write( " <script>alert(' " + ex.Message + " ');</script> " );
24 }
25 }
26 }
2 protected void close_Click( object sender, EventArgs e)
3 {
4 if (Session[ " thread " ] == null )
5 HttpContext.Current.Response.Write( " <script>alert('对不起,线程为空');</script> " );
6 else
7 {
8 try
9 {
10 // 将线程哈希取出
11 Hashtable ht = (Hashtable)Session[ " thread " ];
12 // 停止线程
13 work w = (work)ht[tb.Text.Trim()];
14 w.stop();
15 w = null ;
16 ht.Remove(tb.Text.Trim());
17 // 打印信息
18 PrintThread();
19 HttpContext.Current.Response.Write( " <script>alert('线程 " + tb.Text.Trim() + " 已关闭');</script> " );
20 }
21 catch (Exception ex)
22 {
23 HttpContext.Current.Response.Write( " <script>alert(' " + ex.Message + " ');</script> " );
24 }
25 }
26 }
此方法是将多线程存入到hashtable中进行管理,而不是用线程池.