• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
灬伊天?
博客园    首页    新随笔    联系   管理    订阅  订阅

000-Thread线程池

 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 }

 

posted @ 2018-09-08 19:02  灬伊天☂  阅读(178)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3