我有一个异步方法:
public async Task<UserLoginExResult> LoginExAsync(CustomTable exRequest, string language, bool throwEx = true)
{
UserLoginExResult result = await UserService.LoginExAsync(UserGroup, language, TimezoneOffset, GetDeviceInfo(), GetLoginProperties(), exRequest);
ProcessLoginResult(result, false, throwEx);
return result;
}
Run Code Online (Sandbox Code Playgroud)
过载:
public Task<UserLoginExResult> LoginExAsync(CustomTable exRequest, bool throwEx = true)
{
return LoginExAsync(exRequest, Language.ID, throwEx);
}
Run Code Online (Sandbox Code Playgroud)
我不确定是否应该标记过载的(参数较少的那个)async
并使用await
?我想我不应该但是你能告诉我如果我这样做会发生什么?我在这里很迷茫但不确定Task
它会等待什么?它会创建一个额外的Task
或await
不创建一个新的Task
?
有什么方法可以Page_Load
在使用时重定向(或任何其他ASP.NET事件)async
- await
?当然Redirect
会抛出,ThreadAbortException
但即使我抓住它try
- catch
它最终会出现错误页面.如果我调用Response("url", false)
它不会崩溃,但我需要停止执行页面(渲染页面等),所以这不是解决方案.正如我注意到这两种方法的行为不同:
这最终导致ThreadAbortException(我假设任务同步结束):
protected async void Page_Load()
{
await Task.Run(() => { });
Response.Redirect("http://www.google.com/");
}
Run Code Online (Sandbox Code Playgroud)
这个在Response.Redirect之后继续:
protected async void Page_Load()
{
await Task.Delay(1000);
Response.Redirect("http://www.google.com/");
}
Run Code Online (Sandbox Code Playgroud)
我必须等待响应,但我正在尝试,即使我删除了await
关键字(所以任务在后台运行,方法继续)它最终都是一样的.唯一有帮助的是删除async
关键字 - 我认为async
只能启用await
,仅此而已?!