• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#线程池使用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

使用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());
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C语言整数发布时间:2022-07-13
下一篇:
[转](.NETCoreC#)AESEncryption发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap