async pattern in a top-level worker method-Collection of common programming errors
We have some worker threads in a web role. It is created in the OnStart method
Task.Factory.StartNew(() => DoWorkAsync(), TaskCreationOptions.LongRunning);
Then the DoWorkAsync method will continue polling the queue and do some time consuming work. The actual work takes several minutes as it submits a query job and waits for the result to come back. Those functions are in async pattern. What’s the right way to implement the top-level DoWorkAsync method? If I do the following, then it’s kind of blocking here, and only one PollingQueueAndWorkAsync() is called.
public async void DoWorkAsync()
{
while (_continue)
{
await PollingQueueAndWorkAsync();
}
}
Or the following? It doesn’t look right as it keeps running the method in a different thread.
public async void DoWorkAsync()
{
while (_continue)
{
Task.Run(() => PollingQueueAndWorkAsync());
}
}
I was trying the refactor the code to be “async all the way”. But how should it end in the top-level worker method?
[Update]
After trying more, here is what I have:
public async void DoWorkAsync()
{
while (_continue)
{
Task.Run(async () => await PollingQueueAndWorkAsync());
}
}
The behavior is what I want, since new messages keep adding to the queue, this loop can process as many as messages in the queue. However after running this for a while, it throws our of memory exception. Is it because it creates too many continuations? So I should use SemaphoreSlim to throttle number of tasks?