使用C#线程池可以省去对线程的创建、销毁等操作,但是也不能很好的控制线程,这里是本人对线程池的理解,留下记录方便以后查询!
本DEMO用线程池多线程去读取Northwind所有表的数据与不用线程读取的时间差异
不用线程读取时间:
使用线程读取时间:
主要代码:
1 不使用线程读取代码:
1 private void button4_Click(object sender, EventArgs e)
2 {
3 if (listBox1.Items.Count > 0)
4 {
5 System.Diagnostics.Stopwatch singleThreadWatch = new System.Diagnostics.Stopwatch();
6 singleThreadWatch.Start();
7 tableCount = 0;
8 useTime = 0;
9 label1.Text = "";
10 tabControl1.Controls.Clear();
11 string[] list = new string[listBox1.Items.Count];
12 for (int i = 0; i < listBox1.Items.Count; i++)
13 {
14 list[i] = listBox1.Items[i].ToString();
15 }
16
17 if (list != null && list.Length > 0)
18 {
19 foreach (string item in list)
20 {
21 tableCount += 1;
22
23 using (SqlConnection con = new SqlConnection(connectionString))
24 {
25 if (con.State != ConnectionState.Open) con.Open();
26 string selectAllTableNameSQL = "select * from [" + item.ToString()+"]";
27 using (SqlCommand sqlCmd = new SqlCommand(selectAllTableNameSQL, con))
28 {
29 sqlCmd.CommandType = CommandType.Text;
30 SqlDataReader sqlDataRead = sqlCmd.ExecuteReader();
31 if (null != sqlDataRead)
32 {
33 DataTable dt = new DataTable();
34 dt.Load(sqlDataRead);
35 if (null != dt && dt.Rows.Count > 0)
36 {
37 DataGridView dgv = new DataGridView();
38 dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
39 dgv.Dock = System.Windows.Forms.DockStyle.Fill;
40 dgv.Location = new System.Drawing.Point(3, 3);
41 dgv.Name = "dataGridView" + item.ToString();
42 dgv.RowTemplate.Height = 23;
43
44 dgv.DataSource = dt;
45
46 TabPage tb = new TabPage();
47 tb.Controls.Add(dgv);
48 tb.Location = new System.Drawing.Point(4, 22);
49 tb.Name = "tabPage" + item.ToString();
50 tb.Padding = new System.Windows.Forms.Padding(3);
51 tb.Size = new System.Drawing.Size(358, 446);
52 tb.Text = "tabPage" + item.ToString();
53 tb.UseVisualStyleBackColor = true;
54
55 tabControl1.Controls.Add(tb);
56 Application.DoEvents();
57 }
58 }
59 sqlDataRead.Close();
60 sqlDataRead.Dispose();
61 }
62
63
64 }
65 Application.DoEvents();
66 }
67 }
68
69
70 singleThreadWatch.Stop();
71
72 TimeSpan ts = singleThreadWatch.Elapsed;
73
74 label1.Text = "单线程 表数量:" + list.Length.ToString() + " 总用时间: " + ts.TotalMilliseconds.ToString();
75
76
77 }
78 }
2 使用线程池读取代码:
private void button3_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
tableCount = 0;
useTime = 0;
label1.Text = "";
tabControl1.Controls.Clear();
string[] list = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
list[i] = listBox1.Items[i].ToString();
}
ThreadSart(list);
}
}
private void ThreadSart(object t)
{
string[] objs = t as string[];
if (objs != null && objs.Length > 0)
{
foreach (string item in objs)
{
tableCount += 1;
ThreadPool.QueueUserWorkItem(new WaitCallback(OpenAllTables), item);
Application.DoEvents();
}
}
}
public void OpenAllTables(object obj)
{
OpenDBTables(obj.ToString());
}
private void OpenDBTables(string b)
{
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
using (SqlConnection con = new SqlConnection(connectionString))
{
if (con.State != ConnectionState.Open) con.Open();
string selectAllTableNameSQL = "select * from [" + b.ToString()+"]";
using (SqlCommand sqlCmd = new SqlCommand(selectAllTableNameSQL, con))
{
sqlCmd.CommandType = CommandType.Text;
SqlDataReader sqlDataRead = sqlCmd.ExecuteReader();
if (null != sqlDataRead)
{
DataTable dt = new DataTable();
dt.Load(sqlDataRead);
if (null != dt && dt.Rows.Count > 0)
{
setData(dt,b);
}
}
sqlDataRead.Close();
sqlDataRead.Dispose();
}
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Write(b.ToString() + " 打开用时:" + ts.TotalMilliseconds.ToString());
useTime += ts.TotalMilliseconds;
ShowCountAndTime();
Application.DoEvents();
//int MaxWorkerThreads, miot, AvailableWorkerThreads, aiot;
////获得最大的线程数量
//ThreadPool.GetMaxThreads(out MaxWorkerThreads, out miot);
//AvailableWorkerThreads = aiot = 0;
////获得可用的线程数量
//ThreadPool.GetAvailableThreads(out AvailableWorkerThreads, out aiot);
////返回线程池中活动的线程数
//Write("活动的线程数:" + (MaxWorkerThreads - AvailableWorkerThreads).ToString());
}