C# 多线程 线程调度Dispatcher ,ManualResetEvent
来源:互联网 发布:it is not until 编辑:程序博客网 时间:2024/05/04 22:13
1. ManualResetEvent用法
转之:http://blog.tom.com/blog/read.php?bloggerid=313638&blogid=13505
Thread and Sync In C# (C#中的线程与同步)
别相信别人告诉你的所有的事。其实C#中的线程是很简单的。
线程是程序中的控制流程的封装。你可能已经习惯于写单线程程序,也就是,程序在它们的代码中一次只在一条路中执行。如果你多弄几个线程的话,代码运行可能会更加“同步”。在一个有着多线程的典型进程中,零个或更多线程在同时运行。但是,在有着N个CPU的机器上,一个线程只能在给定的时间上在一个CPU上运行,因为每个线程都是一个代码段,每个CPU一次只能运行一段代码。而看起来像是N个同时完成是线程间共享CPU时间片的效果。这个例子里,我们将创建另一个线程,我们将用两个线程演示多线程的工作方式,最后,我们实现两个线程(主线程与新线程)同步,在新线程工作前必须等待消息。建立线程前我们必须引入System.Threading命名空间。然后我需要知道的是,线程得为控制流程建立一个起点。起点是一个函数,可以使一个相同的调用或其它。
这里你可以看到在同一个类中定义的起点函数。
using System;using System.Threading;namespace ThreadingTester{class ThreadClass{ public static void trmain() { for(int x=0;x < 10;x++) { Thread.Sleep(1000); Console.WriteLine(x); } } static void Main(string[] args) { Thread thrd1=new Thread(new ThreadStart(trmain)); thrd1.Start(); for(int x=0;x < 10;x++) { Thread.Sleep(900); Console.WriteLine("Main :" + x); } }}}
Thread.Sleep(n)方法把“this”线程置于n毫秒的休眠状态。你可以看看这个例子,在主函数我们定义了一个新的线程,其中它的起点是函数trmain(),我们然后包含了Start()方法开始执行。如果你运行这个例子,你就会了解线程间的切换(让CPU从运行一个线程转到另一个线程)让线程几乎同时运行,为了能看哪个线程运行更快我把主线程设置比新线程少100毫秒。
现在,在开始线程前,先给线程命名:
Thread thrd1=new Thread(new ThreadStart(trmain));
thrd1.Name="thread1";
thrd1.Start();
Thread tr = Thread.CurrentThread;
Console.WriteLine(tr.Name);
在完成上面程序后,设想我们不想在一开始新线程就让它马上运行结束,也就是说,我们开启了一个新线程,让它运行,在某个特定的时间点,新线程暂停并等待从主线程(或其他线程)发来的消息。
我们可以这样定义:
public static ManualResetEvent mre = new ManualResetEvent(false);
ManualResetEvent建立时是把false作为start的初始状态,这个类用于通知另一个线程,让它等待一个或多个线程。注意,为了通知或监听同一个线程,所有的其它线程都能访问那个类。
等待线程这样写:
mre.WaitOne();
这将引起等待线程无限期的阻塞并等待类来通知。
发信号的线程应该这样:
mre.Set();
这样类就会被通知,值变成true,等待线程就会停止等待。在通知事件发生后,我们就可以使用下面语句把线程置于基状态:
mre.Reset();
现在让我们在程序执行一下:
using System;using System.Threading;namespace ThreadingTester{class ThreadClass{public static ManualResetEvent mre=new ManualResetEvent(false);public static void trmain(){Thread tr = Thread.CurrentThread;Console.WriteLine("thread: waiting for an event");mre.WaitOne();Console.WriteLine("thread: got an event");for(int x=0;x < 10;x++){Thread.Sleep(1000);Console.WriteLine(tr.Name +": " + x);}} static void Main(string[] args){Thread thrd1=new Thread(new ThreadStart(trmain));thrd1.Name="thread1";thrd1.Start();for(int x=0;x < 10;x++) {Thread.Sleep(900);Console.WriteLine("Main:" + x);if(5==x) mre.Set();}while(thrd1.IsAlive){Thread.Sleep(1000);Console.WriteLine("Main: waiting for thread to stop");}}}}
-----------------------------------------------------
DispatcherObject.CheckAccess 方法
参见msdn https://msdn.microsoft.com/zh-cn/library/system.windows.threading.dispatcherobject.checkaccess.aspx
- .NET Framework 4
- .NET Framework 3.5
- .NET Framework 3.0
确定调用线程是否可以访问此 DispatcherObject。
程序集: WindowsBase(在 WindowsBase.dll 中)
语法
public bool CheckAccess()
返回值
类型:System.Boolean如果调用线程可以访问此对象,则为 true;否则,为false。
备注
只有在其上创建 Dispatcher 的线程才可以访问DispatcherObject。
任何线程都可以检查它是否可以访问此 DispatcherObject。
CheckAccess和 VerifyAccess 之间的区别在于:CheckAccess 返回一个布尔值,此布尔值指定调用线程是否可以访问此DispatcherObject,如果调用线程不能访问此DispatcherObject,则VerifyAccess 将引发异常。
调用此方法等同于在关联的 Dispatcher 对象上调用CheckAccess。
// Uses the DispatcherObject.CheckAccess method to determine if // the calling thread has access to the thread the UI object is onprivate void TryToUpdateButtonCheckAccess(object uiObject){ Button theButton = uiObject as Button; if (theButton != null) { // Checking if this thread has access to the object if(theButton.CheckAccess()) { // This thread has access so it can update the UI thread UpdateButtonUI(theButton); } else { // This thread does not have access to the UI thread // Pushing update method on the Dispatcher of the UI thread theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateUIDelegate(UpdateButtonUI), theButton); } }}
Dispatcher.InvokeAsync 方法 (Action)
执行指定的Action的线程上异步Dispatcher与之关联。
程序集: WindowsBase(在 WindowsBase.dll 中)
语法
public DispatcherOperation InvokeAsync(Action callback)
参数
- callback
- 类型:System.Action
要通过调度程序调用的委托。
返回值
类型:System.Windows.Threading.DispatcherOperation调用 InvokeAsync 之后立即返回的对象,可用来与事件队列中挂起执行的委托进行交互。
- C# 多线程 线程调度Dispatcher ,ManualResetEvent
- C# 多线程 AutoResetEvent和ManualResetEvent
- C#笔记20:多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent
- C#笔记20:多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之ManualResetEvent和AutoResetEvent
- c#多线程之mutex,semaphore,autoresetevent,manualResetevent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#的ManualResetEvent在多线程中的用法
- C#多线程之二:ManualResetEvent和AutoResetEvent
- c#多线程以及Dispatcher.Invoke
- C#线程控制ManualResetEvent和AutoResetEvent
- C#中的线程与同步 ManualResetEvent用法
- 多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent
- C#多线程同步事件及等待句柄AutoResetEvent 和 ManualResetEvent
- C#线程学习(一) AutoResetEvent与ManualResetEvent
- CSS定义鼠标移上图片链接,出现边框效果
- 实验三:学生工作管理系统asp和asp.cs代码
- Lucene5.2.0+SQLite Demo
- NSPoint、NSSize、NSRect、NSRange使用与Bool细节
- Android应用自动更新功能的代码实现
- C# 多线程 线程调度Dispatcher ,ManualResetEvent
- DevExpress中GridControl中实现checkbox多行选中
- SQL Server 2005 Reporting Services (x64) on Windows 2K8 -> CleanCurrentUserName() not found
- ajax替换iframe方法
- HDU 1224 Free DIY Tour--DP--(bug集锦)
- URAL 2035. Another Dress Rehearsal (乱搞)
- IOS应用开发版本控制工具之Versions使用
- 人工智能独立攻克生物领域120年的未解之谜
- 自助Linux之问题诊断工具strace