- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在 Microsoft .NET 中,方法 WaitOne()
public virtual bool WaitOne(
TimeSpan timeout
)
如果当前实例收到信号,将返回true
;否则,false
。
我的问题是,有没有办法让它在超时点还没到的时候返回false
?
或者
换句话说,有没有一种方法可以在WaitOne()
上立即触发超时,即使真正的超时点还没有到来?
更新:
该项目基于.NET 3.5,所以ManualResetEventSlim可能不起作用(在 .NET 4 中引入)。无论如何谢谢@ani。
最佳答案
你不能取消 WaitOne 但你可以包装它:
public bool Wait(WaitHandle yourEvent, WaitHandle cancelEvent, TimeSpan timeOut)
{
WaitHandle[] handles = new WaitHandle[] { yourEvent, cancelEvent };
// WaitAny returns the index of the event that got the signal
var waitResult = WaitHandle.WaitAny(handles, timeOut);
if(waitResult == 1)
{
return false; // cancel!
}
if(waitResult == WaitHandle.WaitTimeout)
{
return false; // timeout
}
return true;
}
只需传递您要等待的句柄和取消等待和超时的句柄即可。
额外
作为一个扩展方法,它可以以类似于 WaitOne 的方式被调用:
public static bool Wait(this WaitHandle yourEvent, WaitHandle cancelEvent, TimeSpan timeOut)
{
WaitHandle[] handles = new WaitHandle[] { yourEvent, cancelEvent };
// WaitAny returns the index of the event that got the signal
var waitResult = WaitHandle.WaitAny(handles, timeOut);
if(waitResult == 1)
{
return false; // cancel!
}
if(waitResult == WaitHandle.WaitTimeout)
{
return false; // timeout
}
return true;
}
关于c# - 有什么方法可以立即触发 WaitOne() 超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22678428/
我需要访问由两个进程共享的 Windows 8.1 应用程序中的资源:应用程序本身和后台任务,所以我需要一个命名的 Semaphore,SemaphoreSlim 确实不适用于那里,因为我在获取和发布
我有一 strip 有 waitOn 的路线返回 Meteor.subscribe 的钩子(Hook).每次触发路线时,我都会从我的 loadingTemplate 中看到微调器在看到实际数据之前简要
我有以下代码 ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadAsync), apiMethod); downloadHandle.Wait
我尝试了一些线程池示例。我从Fibonacci example on MSDN web site开始,但是从this wasn't suitable for more than 64 calculat
我有一个定时器,它的回调做一些事情: 计时器: dataProcessingTimer = new System.Threading.Timer(new TimerCallback(DataProce
我有一个订阅购物车出版物的全局 waitOn。 但是,在购物车页面上,我想显示购物车中的商品列表,购物车有一个带有listingIds的对象的line_items数组。 所以我还需要订阅购物车页面上的
我有一个信号量,它应该限制为 3,但是,它只是想调用多少就调用多少。我假设这是因为我使用 (1000)。但是,当我只尝试 () 时,它永远不会通过 WaitOne,我不确定该怎么做。 private
我正在尝试创建一个 Windows 服务,该服务每 5 分钟轮询一次系统并检查需要完成的某些操作。我已经阅读了 WaitHandles 及其在该领域的实用性,但需要了解其工作原理。 见下面的代码: p
我的应用程序需要每分钟为每个租户执行一些任务。这些是一劳永逸的操作,所以我不想使用 Parallel.ForEach 来处理这个问题。 相反,我循环遍历租户列表,并触发 ThreadPool.Queu
我正在编写一个带有临界区的应用程序。 而我决定使用AutoResetEvent来实现互斥。这是代码 public class MyViewModel { private
想象一下,我有一个类,它的方法可以被多个线程访问。想象一下,该类包含一个整数“i”作为私有(private)字段,并且一些方法会增加/减少该值。 最后想象一下我的一个方法每次 i == 5 时都需要阻
在 Microsoft .NET 中,方法 WaitOne() public virtual bool WaitOne( TimeSpan timeout ) 如果当前实例收到信号,将返回true;否
static Mutex mutex = new Mutex (false, "oreilly.com OneAtATimeDemo"); static void Main() { // Wa
1)调用AsyncWaitHandle.WaitOne可能会阻塞客户端还是一定会阻塞客户端? 2)WaitAll,WaitOne,WaitAny 有什么区别? 最佳答案 WaitHandle.Wait
我对我正在使用的 ManualResetEvent 感到有点困惑,它似乎没有解锁。有人知道为什么会这样吗? 我遇到的情况与此类似。实际情况相当复杂,我没有设法隔离出一段可以合理发布以重现问题的代码。
Threaded 应该创建 4 个独立的线程并等待它们中的每一个直到它们完成。每个线程休眠一段时间,只有当共享互斥对象没有被另一个线程占用时才终止,然后通过一个事件发出它完成的信号(这是我代码的简化版
我尝试在我的 Template.rendered 中获取返回的数据功能。 目前的代码是: this.route('editCat', { layoutTemplate : 'layoutCol
我正在尝试使用互斥锁来保护多个线程对某些硬件的访问,但我对 exitContext 的内容感到困惑参数意味着/做: public virtual bool WaitOne ( int mill
我正在使用 AutoResetEvent,其中可以对一个事件进行多个 Set 调用(异常处理)。有时会调用额外的 Set,因此当代码对 WaitOne 事件进行第二次调用时,它会直接通过,因为门已经打
在此先感谢您的帮助。 我想在代码中的某个时候取消AsyncWaitHandle.WaitOne()(这意味着我不想再阻止了),但我只是找不到解决方法。也许,这是我程序的逻辑问题,但是一旦给出退出信号,
我是一名优秀的程序员,十分优秀!