温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Winform中C#线程控制的常见情况有哪些

发布时间:2021-12-03 10:11:10 来源:亿速云 阅读:117 作者:iii 栏目:编程语言

本篇内容主要讲解“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 <int> 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 <int> _queue;   private SyncEvents _syncEvents;   }    public class Consumer   {   public Consumer(Queue <int> 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 <int> _queue;   private SyncEvents _syncEvents;   }    public class ThreadSyncSample   {   private static void ShowQueueContents(Queue <int> q)   {   lock (((ICollection)q).SyncRoot)   {   foreach (int item in q)   {   Console.Write("{0} ", item);   }   }   Console.WriteLine();   }    static void Main()   {   Queue <int> queue = new Queue <int>();   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 <4; i++)   {   Thread.Sleep(2500);   ShowQueueContents(queue);   }    Console.WriteLine("Signaling threads to terminate...");   syncEvents.ExitThreadEvent.Set();    producerThread.Join();   consumerThread.Join();   }    }

到此,相信大家对“Winform中C#线程控制的常见情况有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI