Question

I have an async task.

async Task UploadFiles()
{
}

I would like to call 'await' on UploadFiles() in Run() method in azure workerrole. but 'await' works only in the methods declared async. So can I make Run() method async like below:

public override void Run()
{
   UploadFiles();
}

to

public async override void Run()
{
   await UploadFiles();
}
Was it helpful?

Solution

Worker roles only have a synchronous entry point. This means that you will need to keep the thread that the Run method runs on active.

You can just call Wait on the task that UploadFiles gives you. Waiting is normally avoided but you are forced to do it here. The cost is not that high: One thread wasted.

OTHER TIPS

As @usr has mentioned, the entry point for a worker role only runs synchronously and so you will need to wait on any task you start. Typically however, I find I usually have more than one task that I want to kick off asynchronously from the worker role. The standard pattern I follow is this

public override void Run()
{
    var tasks = new List<Task>();

    tasks.Add(RunTask1Async());
    tasks.Add(RunTask2Async());
    tasks.Add(RunTask3Async());

    Task.WaitAll(tasks.ToArray());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top