1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Threading;
8 using System.Windows.Forms;
9
10 namespace WindowsFormsApplication
11 {
12 public partial class WinForm : Form
13 {
14 public WinForm()
15 {
16 InitializeComponent();
17 Control.CheckForIllegalCrossThreadCalls = false;
18 }
19 AutoResetEvent myEventIp = new AutoResetEvent(false);
20 AutoResetEvent myEventIpWhile = new AutoResetEvent(false);
21 ReaderWriterLock lockIp = new ReaderWriterLock();
22 int sum = 100, iisIp = 10, ipS = 0;
23 private void button_Click(object sender, EventArgs e)
24 {
25 ThreadPool.SetMinThreads(1, 1);
26 ThreadPool.SetMaxThreads(10, 10);
27 for (int i = 0; i < 100; i++)
28 {
29 ThreadPool.QueueUserWorkItem(new WaitCallback(Function), i.ToString());
30 if (i % 10 == 0)//if (task.IndexOf(item) % 10 == 0)
31 {
32 ipS++;
33 if (ipS >= 2)
34 {
35 myEventIpWhile.WaitOne();
36 MessageBox.Show("---" + i.ToString() + "---");
37 }
38 }
39 }
40 myEventIp.WaitOne();
41 MessageBox.Show("The End!");
42 }
43 public void Function(object obj)
44 {
45 lockIp.AcquireWriterLock(10000);
46 MessageBox.Show(obj.ToString());
47 lockIp.ReleaseWriterLock();
48 if (ipS >= 2)
49 {
50 iisIp -= 1;
51 if (iisIp == 0)
52 {
53 myEventIpWhile.Set();
54 iisIp = 10;
55 }
56 }
57 sum -= 1;
58 if (sum == 0)
59 {
60 myEventIp.Set();
61 }
62 }
63 }
64 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Threading;
5 using System.Windows.Forms;
6
7 namespace SuperDLL
8 {
9 public static class Thread
10 {
11 public static void MultiThread()
12 {
13 Thread thread = new Thread(new ThreadStart(Function));
14 thread.IsBackground = true;
15 thread.Start();
16 thread.Join(2000);
17 MessageBox.Show("完成");
18 }
19 public static void Function()
20 {
21 Thread.Sleep(1000);
22 MessageBox.Show("方法");
23 }
24 public static void MultiThreading()
25 {
26 Thread thread = new Thread(new ParameterizedThreadStart(Function));
27 thread.IsBackground = true;
28 thread.Start("Transmission");
29 thread.Join(2000);
30 thread.Abort();
31 MessageBox.Show("完成");
32 }
33 public static void Function(object obj)
34 {
35 Thread.Sleep(1000);
36 MessageBox.Show(obj.ToString());
37 }
38 }
39 }