【发布时间】:2019-10-02 13:46:28
【问题描述】:
这与必须从网络上的服务器收集一些信息的程序有关。
问题:如何从 GetServerResponseAsync 和 Check 中获取异常以通过 CheckAsync 冒泡到主程序?
如图所示,它们没有。 ErrorHandler 永远不会被击中。
我的主程序:
....
try
{
Task.Run(() => CheckAsync());
// bubble to here?
ReadConfiguration();
}
catch (Exception ex)
{
// gather and handle all exceptions here
ErrorHandler.NotifyMe(new[] { "some message" }, ErrorType.Stop); // never gets hit
}
public async Task CheckAsync()
{
await GetServerResponseAsync("slm_check"); // may throw exception
...
if (.....)
throw new Exception("...");
...
}
public async Task GetServerResponseAsync(string command)
{
...
// client = HttpClient()
using (apacheResponse = await client.GetAsync(ServerUrl + "...."))
{
if (....)
throw new Exception("Internal web server error", new Exception("Maybe MySQL server is down"));
using (HttpContent content = apacheResponse.Content)
{
if ( ....)
throw new Exception("error message");
}
}
}
【问题讨论】:
-
await Task.Run(...)甚至更好的await CheckAsync() -
你为什么在后台线程上调用
CheckAsync?删除Task.Run。
标签: c# asynchronous exception