当前位置: 首页 > wzjs >正文

企业建设高端网站的目的重庆森林电影

企业建设高端网站的目的,重庆森林电影,网站开发和网络设计有什么区别,上传到ftp网站模板默认情况下,一个线程的特定指令相对于另一个线程中的指令的执行时机是不确定的,如果想要对这种不确定性进行控制,其中一种办法就是使用重置事件(虽然称为事件,但是跟C#的委托跟事件没关系)。 重置事件用于…

默认情况下,一个线程的特定指令相对于另一个线程中的指令的执行时机是不确定的,如果想要对这种不确定性进行控制,其中一种办法就是使用重置事件(虽然称为事件,但是跟C#的委托跟事件没关系)。

重置事件用于强迫代码等候另一个线程的执行,直到获得事件已发生的通知。

重置事件类型包括ManualResetEventManualResetEventSlim以及AutoResetEvent,但在使用过程中应该尽量用前面两种类型,而避免使用AutoResetEvent

ManualResetEvent

ManualResetEvent对象具有两种状态,非信号状态信号状态

  • ManualResetEvent对象处于非信号状态,调用WaitOne方法,则当前线程会被阻塞,直到此ManualResetEvent对象调用Set方法后线程才被释放。
  • ManualResetEvent对象处于信号状态,调用WaitOne方法时,当前线程不会被阻塞,ManualResetEvent立即释放线程并返回到非信号状态。

一、常用成员

构造函数

ManualResetEvent(Boolean)ManualResetEvent的构造函数,参数为false则创建的ManualResetEvent对象为非信号状态,反之为信号状态。

常用方法

boolean WaitOne():如果当前ManualResetEvent对象为非信号状态,阻塞当前线程,直到收到信号(调用Set()方法);如果当前的ManualResetEvent对象为信号状态,则不会阻塞当前线程。

  • 收到信号或当前对象为信号状态时返回true,否则一直等待,不会返回。

bool WaitOne([int millisecondsTimeout]):如果当前ManualResetEvent对象为非信号状态,阻止当前线程,直到超过指定时间或收到信号(某处调用了此对象的Set());如果当前的ManualResetEvent对象为信号状态,则不会阻塞当前线程。

  • 收到信号返回true,否则返回false

bool Set():将状态设置为信号状态,允许一个或多个等待线程继续。

  • 操作成功返回true,否则false

bool Reset():将状态设置为非信号状态。

  • 如果该操作成功,返回true,否则false

Dispose():释放WaitHandle类(ManualResetEvent为其子类)的当前实例所使用的所有资源。

二、示例

ManualResetEvent manualResetEvent= new ManualResetEvent(false);Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(new IPEndPoint(IPAddress.Any, 9090));
server.Listen();server.BeginAccept(new AsyncCallback(asyncResult =>
{Socket s = asyncResult.AsyncState as Socket;Socket ct = s.EndAccept(asyncResult);while (true){manualResetEvent.Reset();byte[] buffer = new byte[1024];ct.BeginReceive(buffer, 0, 1024, SocketFlags.None, new AsyncCallback(asyncResult =>{Socket c = asyncResult.AsyncState as Socket;c.EndReceive(asyncResult);manualResetEvent.Set();}), ct);manualResetEvent.WaitOne();Console.WriteLine(Encoding.UTF8.GetString(buffer));}
}), server);Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9090));
for (int i = 0; i < 10; i++)
{//Thread.Sleep(20); //如果有个延时就可以看到不粘包client.Send(Encoding.UTF8.GetBytes("Hello"));
}Console.ReadLine();

三、注意

释放资源

官方文档中说明:实现 IDisposable接口。 在使用完类型后,应直接或间接释放类型。 若要直接释放类型,请在 try/catch块中调用其Dispose方法。 若要间接释放类型,请使用 using。 因此在使用过程中,可以的话尽量考虑资源的释放。

ManualResetEventSlim

ManualResetEventManualResetEventSlim的区别在于,前者默认使用核心同步,而后者进行了优化,除非万不得已,否则会尽量避免使用核心机制,因此ManualResetEventSlim的性能更好。因此一般情况下应选用ManualResetEventSlim,除非要等待多个事件或需跨越多个进程。

此外,用了一下发现,ManualResetEventSlim用的是Wait()而不是WaitOne(),两者比较大的区别在于Wait()还可以接收一个取消令牌。

AutoResetEvent

AutoResetEvent类表示线程同步事件在一个等待线程释放后收到信号时自动重置

注意,AutoResetEvent对象调用Set()后会自动调用Reset()方法,这也是类名的由来,而ManualResetEvent其功能跟AutoResetEvent是一样的,只是在调用Set()方法后不会自动调用Reset()方法。此外,AutoResetEvent只解除一个线程的Wait()调用所造成的阻塞,使用自动重置事件,很容易在编写生产者线程时发生失误,导致它的迭代次数多于消费者线程。

实际上,应该尽量避免使用AutoResetEvent,而选择使用ManualResetEventManualResetEventSlim

  • 官方示例

using System;
using System.Threading;// Visual Studio: Replace the default class in a Console project with 
//                the following class.
class Example
{private static AutoResetEvent event_1 = new AutoResetEvent(true);private static AutoResetEvent event_2 = new AutoResetEvent(false);static void Main(){Console.WriteLine("Press Enter to create three threads and start them.\r\n" +"The threads wait on AutoResetEvent #1, which was created\r\n" +"in the signaled state, so the first thread is released.\r\n" +"This puts AutoResetEvent #1 into the unsignaled state.");Console.ReadLine();for (int i = 1; i < 4; i++){Thread t = new Thread(ThreadProc);t.Name = "Thread_" + i;t.Start();}Thread.Sleep(250);for (int i = 0; i < 2; i++){Console.WriteLine("Press Enter to release another thread.");Console.ReadLine();event_1.Set();Thread.Sleep(250);}Console.WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");for (int i = 0; i < 3; i++){Console.WriteLine("Press Enter to release a thread.");Console.ReadLine();event_2.Set();Thread.Sleep(250);}// Visual Studio: Uncomment the following line.//Console.Readline();}static void ThreadProc(){string name = Thread.CurrentThread.Name;Console.WriteLine("{0} waits on AutoResetEvent #1.", name);event_1.WaitOne();Console.WriteLine("{0} is released from AutoResetEvent #1.", name);Console.WriteLine("{0} waits on AutoResetEvent #2.", name);event_2.WaitOne();Console.WriteLine("{0} is released from AutoResetEvent #2.", name);Console.WriteLine("{0} ends.", name);}
}
  • 实际用例
    下面是下载文件的一部分代码,遍历文件列表,每一个文件开始下载后就阻塞当前线程,直到文件下载完成后,再释放线程。
private AutoResetEvent autoResetEvent = new AutoResetEvent(false);
private void OnStart()
{Task.Run(() =>{Files.ToList().ForEach(f =>{WebAccess webAccess = new WebAccess();webAccess.DownloadProgressChanged = (int progressPercent) =>{Progress = progressPercent; //设置进度泡泡};webAccess.DownLoadCompleted = () =>{autoResetEvent.Set();};webAccess.Download(f, $@".\{f.FileName}");autoResetEvent.WaitOne();});});
}
http://www.dtcms.com/wzjs/445913.html

相关文章:

  • 电子商务网站建设与管理B卷seo排名优化培训
  • 国外色情网站海外销售平台有哪些
  • 钦州市住房和城乡建设局网站智慧软文发稿平台官网
  • 建设移动网站seo论坛站长交流
  • 开发区网站建设的目的企业网站营销优缺点
  • 住房城乡建设部网站主页西安网站维护
  • 网站建设费税收分类抖音关键词排名
  • 网站视频主持人推广
  • 下载好看影视大全极速版河南百度关键词优化排名软件
  • 网站开发与设计教程seo在线排名优化
  • 辽宁网站建站百度官网下载安装免费
  • 国外产品网站杭州seo薪资水平
  • 怎么做独立网站最近社会热点新闻事件
  • 重庆网站备案在那里东莞seo网站排名优化
  • 微信认证 网站北京网络排名优化
  • 网站建站基础3d建模培训学校哪家好
  • 禹州做网站bz3399营销推广投放平台
  • 湖南网站推广seo优化技巧有哪些
  • 360做企业网站多少钱黑帽seo寄生虫
  • dede的网站地图要怎么做网络推广需要什么
  • 做美工一般用到的素材网站网站收录登录入口
  • 做网上招聘哪个网站好智慧软文
  • 网站建设服务价格表查关键词热度的网站
  • 有什么网站可以做团购搜索引擎优化效果
  • 自己做网站前期困难吗好口碑关键词优化地址
  • 优秀网页设计作品网站泰州seo网络公司
  • 自做视频网站赚钱吗域名解析查询
  • 蜜雪冰城网络营销论文手机优化游戏性能的软件
  • 免费注册域名网站知乎河北企业网站建设
  • java web做网站网站建设有多少公司