织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>.NET(C#)>

多线程执行多任务的DEMO

jackyhwei 发布于 2010-09-15 15:17 点击:次 
应朋友之邀,写了一个多线程执行多任务的例子。这个场景应用比较普遍,比如多个线程下载多个文件,比如3个线程下载10个文件,比如10个线程执行1000条任务队列;
TAG:

应朋友之邀,写了一个多线程执行多任务的例子。这个场景应用比较普遍,

比如多个线程下载多个文件,比如3个线程下载10个文件,比如10个线程执行1000条任务队列;

Demo代码如下:

  1. public partial class Form1 : Form 
  2. private ThreadProxy _threadsProxy = null
  3. ExpressCollection list = new ExpressCollection(); 
  4.  
  5. public Form1() 
  6. InitializeComponent(); 
  7.  
  8. _threadsProxy = new ThreadProxy(3, list); 
  9. _threadsProxy.ExpressComputed += new
  10.  EventHandler<ExpressComputedEventArgs>(_threadsProxy_ExpressComputed); 
  11.  
  12. private void button1_Click(object sender, EventArgs e) 
  13. list.Clear(); 
  14. _threadsProxy.Stop(); 
  15. list.Add(new Express(1, 1)); 
  16. list.Add(new Express(1, 2)); 
  17. list.Add(new Express(1, 3)); 
  18. list.Add(new Express(1, 4)); 
  19. list.Add(new Express(1, 5)); 
  20. list.Add(new Express(1, 6)); 
  21. list.Add(new Express(1, 7)); 
  22. list.Add(new Express(1, 8)); 
  23. list.Add(new Express(1, 9)); 
  24. list.Add(new Express(1, 10)); 
  25. list.Add(new Express(1, 11)); 
  26. list.Add(new Express(1, 12)); 
  27. list.Add(new Express(1, 13)); 
  28. list.Add(new Express(1, 14)); 
  29. Console.WriteLine("启动线程..........................."); 
  30. _threadsProxy.Run(); 
  31.  
  32. private void button2_Click(object sender, EventArgs e) 
  33. if (_threadsProxy != null
  34. Console.WriteLine("测试暂停..........................."); 
  35. _threadsProxy.Stop(); 
  36.  
  37. void _threadsProxy_ExpressComputed(object sender, ExpressComputedEventArgs e) 
  38. Console.WriteLine(string.Format("ThreadID:{0}  {1} + {2} = {3}",
  39.  Thread.CurrentThread.ManagedThreadId, e.Express.A, e.Express.B, e.Result)); 
  40. }public class ThreadProxy : IDisposable 
  41. private int _threadCount = 1; 
  42. ExpressCollection _list; 
  43. private Thread[] _threads = null
  44. private bool _isRun = false
  45.  
  46. public ThreadProxy(ExpressCollection list) 
  47. _list = list; 
  48.  
  49. public ThreadProxy(int threadCount, ExpressCollection list) 
  50. this(list) 
  51. _threadCount = threadCount; 
  52. _threads = CreateThreadCollection(); 
  53.  
  54. public void Run() 
  55. if (!_isRun && _list != null && _list.Count>0) 
  56. _isRun = true
  57. for (int i = 0; i < _threadCount; i++) 
  58. _threads[i] = new Thread(new ThreadStart(ThreadInvoke)); 
  59. _threads[i].IsBackground = true
  60. _threads[i].Start(); 
  61.  
  62. protected virtual Thread[] CreateThreadCollection() 
  63. return new Thread[_threadCount]; 
  64.  
  65. public void Stop() 
  66. _isRun = false
  67.  
  68. private void ThreadInvoke() 
  69. try 
  70. while (_isRun) 
  71. Express express = _list.GetNext(); 
  72. if (express != null
  73. int result = express.A + express.B; 
  74. OnExpressComputed(result, express); 
  75. //do something 
  76. Thread.Sleep(1000);//测试暂停 
  77. else 
  78. break
  79. catch (ThreadAbortException) 
  80. Thread.ResetAbort(); 
  81. return
  82. catch (Exception) 
  83. //记录日志 
  84.  
  85. public event EventHandler<ExpressComputedEventArgs> ExpressComputed; 
  86.  
  87. protected virtual void OnExpressComputed(int result ,Express express) 
  88. if (ExpressComputed != null
  89. ExpressComputed(thisnew ExpressComputedEventArgs(result,express)); 
  90.  
  91. #region IDisposable 成员 
  92.  
  93. public void Dispose() 
  94. _isRun = false
  95. if (_threads != null
  96. _threads = null
  97.  
  98. #endregion 
  99.  
  100. public class ExpressComputedEventArgs : EventArgs 
  101. private int result; 
  102.  
  103. public int Result 
  104. get { return result; } 
  105. set { result = value; } 
  106.  
  107. public ExpressComputedEventArgs(int result , Express express) 
  108. this.result = result; 
  109. this.express = express; 
  110.  
  111. private Express express; 
  112.  
  113. public Express Express 
  114. get { return express; } 
  115. set { express = value; } 
  116. public class BaseCollection<T> : List<T> 
  117. protected readonly object _lockObj = new object(); 
  118.  
  119. private int index = 0; 
  120. public virtual T GetNext() 
  121. T result = default(T); 
  122. if (this.Count > 0 && index < Count) 
  123. lock (_lockObj){ 
  124. result = this[index]; 
  125. index++; 
  126. return result; 
  127.  
  128. public new void Add(T item) 
  129. lock (_lockObj) 
  130. base.Add(item); 
  131.  
  132. public new void Clear() 
  133. lock (_lockObj) 
  134. index = 0; 
  135. base.Clear(); 
  136. public class Express 
  137. private int _a; 
  138.  
  139. public int A 
  140. get { return _a; } 
  141. set { _a = value; } 
  142.  
  143. private int _b; 
  144.  
  145. public int B 
  146. get { return _b; } 
  147. set { _b = value; } 
  148.  
  149. public Express(int a, int b) 
  150. _a = a; 
  151. _b = b; 
  152.  
  153. public class ExpressCollection : BaseCollection<Express> 
(michael-zhangyu)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201009/10159.html]
本文出处:博客园 作者:michael-zhangyu
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容