您当前的位置:首页 > 科普小常识 > 正文

resetevent(C#里 有关线程 ManualResetEvent的问题)

另一种是函数在执行到某一个点的时候停下来等待主线程给他一个信号来决定该执行怎样的逻辑,第一种是主线程向thA发送中止线程指令,ManualResetEvent 允许线程通过发信号互相通信,它调用 Set 以发出等待线程可以继续进行的信号,C#里 有关线程 ManualResetEvent的问题我的理解这是一个线程间通讯的问题,一般用来扩展原型的时候用~或者用到原型链式的继承上去~举个例子:声明一个对象var abc=function(){ this.name=“小明“;}abc现在是一个对象(函数也是对象的一种),此通信涉及一个线程在其他线程进行之前必须完成的任务,接收到a的时候主线程创建一个新的线程。

C#里 有关线程 ManualResetEvent的问题

我的理解这是一个线程间通讯的问题,接收到a的时候主线程创建一个新的线程,运行的函数(比如叫WorkRun方法)在新线程(比如叫thA)中运行,当主线程接收到b时需要通知thA使他中止或改变其运行逻辑。启动线程的写法:Thread thA = new Thread(new ThreadStart(WorkRun));thA.Start();事实上,只有两种可能的场景,第一种是主线程向thA发送中止线程指令,让函数中止执行;另一种是函数在执行到某一个点的时候停下来等待主线程给他一个信号来决定该执行怎样的逻辑。1. 如果是中止线程的话,只需要在接收到b的时候写if(thA.ThreadState == ThreadState.Running){thA.Abort();}2. 如果需要在运行中停下来等待主线程指令的话,使用ManualResetEvent,ManualResetEvent 允许线程通过发信号互相通信。通常,此通信涉及一个线程在其他线程进行之前必须完成的任务。当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时,它调用 Reset 以将 ManualResetEvent 置于非终止状态。此线程可被视为控制 ManualResetEvent。调用 ManualResetEvent 上的 WaitOne 的线程将阻止,并等待信号。当控制线程完成活动时,它调用 Set 以发出等待线程可以继续进行的信号。并释放所有等待线程。一旦它被终止,ManualResetEvent 将保持终止状态,直到它被手动重置。即对 WaitOne 的调用将立即返回。可以通过将布尔值传递给构造函数来控制 ManualResetEvent 的初始状态,如果初始状态处于终止状态,为 true;否则为 false。ManualResetEvent 也可以同 staticWaitAll 和 WaitAny 方法一起使用。以下为实例(下面的代码示例阐释了如何使用等待句柄来发送复杂数字计算的不同阶段的完成信号。计算的格式为:结果 = 第一项 + 第二项 + 第三项,其中每一项都要求使用计算出的基数进行预计算和最终计算。):using System;using System.Threading;class CalculateTest{ static void Main() { Calculate calc = new Calculate(); Console.WriteLine(“Result = .“, calc.Result(234).ToString()); Console.WriteLine(“Result = .“, calc.Result(55).ToString()); }}class Calculate{ double baseNumber, firstTerm, secondTerm, thirdTerm; AutoResetEvent autoEvents; ManualResetEvent manualEvent; // Generate random numbers to simulate the actual calculations. Random randomGenerator; public Calculate() { autoEvents = new AutoResetEvent { new AutoResetEvent(false), new AutoResetEvent(false), new AutoResetEvent(false) }; manualEvent = new ManualResetEvent(false); } void CalculateBase(object stateInfo) { baseNumber = randomGenerator.NextDouble(); // Signal that baseNumber is ready. manualEvent.Set(); } // The following CalculateX methods all perform the same // series of steps as commented in CalculateFirstTerm. void CalculateFirstTerm(object stateInfo) { // Perform a precalculation. double preCalc = randomGenerator.NextDouble(); // Wait for baseNumber to be calculated. manualEvent.WaitOne(); // Calculate the first term from preCalc and baseNumber. firstTerm = preCalc * baseNumber * randomGenerator.NextDouble(); // Signal that the calculation is finished. autoEvents.Set(); } void CalculateSecondTerm(object stateInfo) { double preCalc = randomGenerator.NextDouble(); manualEvent.WaitOne(); secondTerm = preCalc * baseNumber * randomGenerator.NextDouble(); autoEvents.Set(); } void CalculateThirdTerm(object stateInfo) { double preCalc = randomGenerator.NextDouble(); manualEvent.WaitOne(); thirdTerm = preCalc * baseNumber * randomGenerator.NextDouble(); autoEvents.Set(); } public double Result(int seed) { randomGenerator = new Random(seed); // Simultaneously calculate the terms. ThreadPool.QueueUserWorkItem( new WaitCallback(CalculateBase)); ThreadPool.QueueUserWorkItem( new WaitCallback(CalculateFirstTerm)); ThreadPool.QueueUserWorkItem( new WaitCallback(CalculateSecondTerm)); ThreadPool.QueueUserWorkItem( new WaitCallback(CalculateThirdTerm)); // Wait for all of the terms to be calculated. WaitHandle.WaitAll(autoEvents); // Reset the wait handle for the next calculation. manualEvent.Reset(); return firstTerm + secondTerm + thirdTerm; }}

javascript window.history.go(-1);返回刷新不执行呢是不是你们的BUG啊

  您好!很高兴为您答疑。 火狐浏览器关于web页面的方法说明如下:  History.go()Loads a page from the session history, identified by its relative location to the current page, for example -1 for the previous page or 1 for the next page. When integerDelta is out of bounds (e.g. -1 when there are no previously visited pages in the session history), the method doesn’t do anything and doesn’t raise an exception. Calling go() without parameters or with a non-integer argument has no effect (unlike Internet Explorer, which supports string URLs as the argument). 据此可知其本身没有刷新不刷新页面之说,而是Loads a page from the session history,如果您需要更多的说明,可以参阅:  如果对我们的回答存在任何疑问,欢迎继续问询。

batterystatustooweak是什么意思

battery status too weak电池状态太弱.-----------------------------------如有疑问欢迎追问!满意请点击右上方【选为满意回答】按钮

JavaScript里面的prototype是什么意思怎么用能举个例子吗

翻译一下是原型的意思~也就是说是JS对象的原型,一般用来扩展原型的时候用~或者用到原型链式的继承上去~举个例子:声明一个对象var abc=function(){ this.name=“小明“;}abc现在是一个对象(函数也是对象的一种),然后我们扩展一下这个对象,给它一个方法:abc.prototype.say=function(){ alert(this.name);}这样就扩展了原来的函数对象了~是用的时候直接var a=new abc();a.say();就会弹出小明来了。这么做的好处是不会额外产生内存,所有实例化后的对象都会从原型上继承这个方法。至于原型链继承的话你可以去搜一下我就不举例子了~不懂的话可以再问我~


声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,谢谢。

上一篇: 梢棒意思是什么(梢棒什么意思)

下一篇: 三星低端手机(三星哪几款手机最具性价比)



推荐阅读