Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common situations of C # thread control in Winform

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

本篇内容主要讲解"Winform中C#线程控制的常见情况有哪些",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"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

using System; using System.Threading; using System.Collections; using System.Collections.Generic; public class SyncEvents { public SyncEvents() { _newItemEvent = new AutoResetEvent(false); _exitThreadEvent = new ManualResetEvent(false); _eventArray = new WaitHandle[2]; _eventArray[0] = _newItemEvent; _eventArray[1] = _exitThreadEvent; } public EventWaitHandle ExitThreadEvent { get { return _exitThreadEvent; } } public EventWaitHandle NewItemEvent { get { return _newItemEvent; } } public WaitHandle[] EventArray { get { return _eventArray; } } private EventWaitHandle _newItemEvent; private EventWaitHandle _exitThreadEvent; private WaitHandle[] _eventArray; } public class Producer { public Producer(Queue q, SyncEvents e) { _queue = q; _syncEvents = e; } // Producer.ThreadRun public void ThreadRun() { int count = 0; Random r = new Random(); while (!_syncEvents.ExitThreadEvent.WaitOne(0, false)) { lock (((ICollection)_queue).SyncRoot) { while (_queue.Count < 20) { _queue.Enqueue(r.Next(0,100)); _syncEvents.NewItemEvent.Set(); count++; } } } Console.WriteLine("Producer thread: produced {0} items", count); } private Queue _queue; private SyncEvents _syncEvents; } public class Consumer { public Consumer(Queue q, SyncEvents e) { _queue = q; _syncEvents = e; } // Consumer.ThreadRun public void ThreadRun() { int count = 0; while (WaitHandle.WaitAny(_syncEvents.EventArray) != 1) { lock (((ICollection)_queue).SyncRoot) { int item = _queue.Dequeue(); } count++; } Console.WriteLine("Consumer Thread: consumed {0} items", count); } private Queue _queue; private SyncEvents _syncEvents; } public class ThreadSyncSample { private static void ShowQueueContents(Queue q) { lock (((ICollection)q).SyncRoot) { foreach (int item in q) { Console.Write("{0} ", item); } } Console.WriteLine(); } static void Main() { Queue queue = new Queue (); SyncEvents syncEvents = new SyncEvents(); Console.WriteLine("Configuring worker threads..."); Producer producer = new Producer(queue, syncEvents); Consumer consumer = new Consumer(queue, syncEvents); Thread producerThread = new Thread(producer.ThreadRun); Thread consumerThread = new Thread(consumer.ThreadRun); Console.WriteLine("Launching producer and consumer threads..."); producerThread.Start(); consumerThread.Start(); for (int i=0; i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report