'The simplest way to signal any WaitHandle after specified time
I'm looking for any simple way to have any waitable object to be signaled after specified time. Now I'm using something like:
int delayMs = 500; // milliseconds
AutoResetEvent resetEvent = new(false);
using Task autoSignalTask = Task.Run(() =>
{
Thread.Sleep(delayMs);
resetEvent.Set();
});
but it seems to be too complicated ;)
Is there something simpler, like below?
int delayMs = 500; // milliseconds
SomeWaitableObject waitableObject = new();
waitableObject.SignalAfter(delayMs);
Solution 1:[1]
In terms of your initial sample, it would look like
int delayMs = 500; // milliseconds
AutoResetEvent resetEvent = new(false);
// await Task.Delay(delayMs); // the author need to do it sync
Thread.Sleep(delayMs);
resetEvent.Set();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
