AZURE: async Run() in workerrole

旧巷老猫 提交于 2019-12-10 11:25:36

问题


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();
}

回答1:


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.




回答2:


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());
}


来源:https://stackoverflow.com/questions/23194842/azure-async-run-in-workerrole

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!