I have a Windows Service that uses Thread and SemaphoreSlim to perform some "work" every 60 seconds.
class Daemon
{
private SemaphoreSlim _semaphore;
private Thread _thread;
public void Stop()
{
_semaphore.Release();
_thread.Join();
}
public void Start()
{
_semaphore = new SemaphoreSlim(0);
_thread = new Thread(DoWork);
_thread.Start();
}
private void DoWork()
{
while (true)
{
// Do some work here
// Wait for 60 seconds, or exit if the Semaphore is released
if (_semaphore.Wait(60 * 1000))
{
return;
}
}
}
}
I'd like to call an asynchronous method from DoWork
. In order to use the await
keyword I must add async
to DoWork
:
private async void DoWork()
- Is there any reason not to do this?
- Is DoWork actually able to run asynchronously, if it's already running inside a dedicated thread?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…