Winform中C#线程控制的四种常见情况分析

Winform界面中,将事务放在新开的线程中操作是十分有用的做法,因为这样可以增加用户体验,减少耗时。对这些C#线程的控制,常常有下面这四种情况:

成都创新互联公司服务项目包括秦淮网站建设、秦淮网站制作、秦淮网页制作以及秦淮网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,秦淮网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到秦淮省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

1. 启动线程;

2. 线程间通讯;

3. 线程终止;

4. 线程中的异常处理;

下面总结一些上面这些C#线程操作的常用方法。

C#线程控制1. 启动C#线程

◆如果是需要很频繁的开线程,会使用线程池(微软的或自己写的)

◆Thread.Start(参数object);

◆或者用对象提供的BeginXXXX()这种都是异步,也算多线程启动.

C#线程控制2. C#线程间通讯

◆委托,事件这些比较常用,并且对object的多线程处理需要谨慎,可能用到lock(object){}.

◆主要是通过线程同步或者回调方法(或者说是委托)来实现

C#线程控制3. 线程终止

◆线程的终止,用事件AUTORESET之类

◆可以用Thread.ManualEvent().Reset()/Set()/WaitOne()方法来判断和等待

C#线程控制4. 线程中的异常处理

◆线程中的异常通过事件传回到主线程处理

◆还是写log吧,多线程debug比较难,还是逐步log比较好.

用于C#线程通讯的lock关键字

下面的示例演示使用 lock 关键字以及 AutoResetEvent 和 ManualResetEvent 类对主线程和两个辅助线程进行线程同步。

该示例创建两个辅助线程。一个线程生成元素并将它们存储在非线程安全的泛型队列中。有关更多信息,请参见 Queue。另一个线程使用此队列中的项。另外,主线程定期显示队列的内容,因此该队列被三个线程访问。lock 关键字用于同步对队列的访问,以确保队列的状态没有被破坏。

除了用 lock 关键字来阻止同时访问外,还用两个事件对象提供进一步的同步。一个事件对象用来通知辅助线程终止,另一个事件对象由制造者线程用来在有新项添加到队列中时通知使用者线程。这两个事件对象封装在一个名为 SyncEvents 的类中。这使事件可以轻松传递到表示制造者线程和使用者线程的对象。SyncEvents 类是按如下方式定义的:

C# code

 
 
 
  1. using System; 
  2. using System.Threading; 
  3. using System.Collections; 
  4. using System.Collections.Generic; 
  5. public class SyncEvents 
  6. { 
  7. public SyncEvents() 
  8. { 
  9. _newItemEvent = new AutoResetEvent(false); 
  10. _exitThreadEvent = new ManualResetEvent(false); 
  11. _eventArray = new WaitHandle[2]; 
  12. _eventArray[0] = _newItemEvent; 
  13. _eventArray[1] = _exitThreadEvent; 
  14. } 
  15. public EventWaitHandle ExitThreadEvent 
  16. { 
  17. get { return _exitThreadEvent; } 
  18. } 
  19. public EventWaitHandle NewItemEvent 
  20. { 
  21. get { return _newItemEvent; } 
  22. } 
  23. public WaitHandle[] EventArray 
  24. { 
  25. get { return _eventArray; } 
  26. } 
  27. private EventWaitHandle _newItemEvent; 
  28. private EventWaitHandle _exitThreadEvent; 
  29. private WaitHandle[] _eventArray; 
  30. } 
  31. public class Producer 
  32. { 
  33. public Producer(Queue  q, SyncEvents e) 
  34. { 
  35. _queue = q; 
  36. _syncEvents = e; 
  37. } 
  38. // Producer.ThreadRun 
  39. public void ThreadRun() 
  40. { 
  41. int count = 0; 
  42. Random r = new Random(); 
  43. while (!_syncEvents.ExitThreadEvent.WaitOne(0, false)) 
  44. { 
  45. lock (((ICollection)_queue).SyncRoot) 
  46. { 
  47. while (_queue.Count < 20) 
  48. { 
  49. _queue.Enqueue(r.Next(0,100)); 
  50. _syncEvents.NewItemEvent.Set(); 
  51. count++; 
  52. } 
  53. } 
  54. } 
  55. Console.WriteLine("Producer thread: produced {0} items", count); 
  56. } 
  57. private Queue  _queue; 
  58. private SyncEvents _syncEvents; 
  59. } 
  60. public class Consumer 
  61. { 
  62. public Consumer(Queue  q, SyncEvents e) 
  63. { 
  64. _queue = q; 
  65. _syncEvents = e; 
  66. } 
  67. // Consumer.ThreadRun 
  68. public void ThreadRun() 
  69. { 
  70. int count = 0; 
  71. while (WaitHandle.WaitAny(_syncEvents.EventArray) != 1) 
  72. { 
  73. lock (((ICollection)_queue).SyncRoot) 
  74. { 
  75. int item = _queue.Dequeue(); 
  76. } 
  77. count++; 
  78. } 
  79. Console.WriteLine("Consumer Thread: consumed {0} items", count); 
  80. } 
  81. private Queue  _queue; 
  82. private SyncEvents _syncEvents; 
  83. } 
  84. public class ThreadSyncSample 
  85. { 
  86. private static void ShowQueueContents(Queue  q) 
  87. { 
  88. lock (((ICollection)q).SyncRoot) 
  89. { 
  90. foreach (int item in q) 
  91. { 
  92. Console.Write("{0} ", item); 
  93. } 
  94. } 
  95. Console.WriteLine(); 
  96. } 
  97. static void Main() 
  98. { 
  99. Queue  queue = new Queue (); 
  100. SyncEvents syncEvents = new SyncEvents(); 
  101. Console.WriteLine("Configuring worker threads..."); 
  102. Producer producer = new Producer(queue, syncEvents); 
  103. Consumer consumer = new Consumer(queue, syncEvents); 
  104. Thread producerThread = new Thread(producer.ThreadRun); 
  105. Thread consumerThread = new Thread(consumer.ThreadRun); 
  106. Console.WriteLine("Launching producer and consumer threads...");
  107. producerThread.Start(); 
  108. consumerThread.Start(); 
  109. for (int i=0; i <4; i++) 
  110. { 
  111. Thread.Sleep(2500); 
  112. ShowQueueContents(queue); 
  113. } 
  114. Console.WriteLine("Signaling threads to terminate..."); 
  115. syncEvents.ExitThreadEvent.Set(); 
  116. producerThread.Join(); 
  117. consumerThread.Join(); 
  118. } 
  119. }

网页题目:Winform中C#线程控制的四种常见情况分析
URL标题:http://wkslyf.com/article/cddsecc.html