gpt4 book ai didi

c# - 通过等待任务或访问其 Exception 属性未观察到任务的异常

转载 作者:太空狗 更新时间:2023-10-30 00:55:51 25 4
gpt4 key购买 nike

这些是我的任务。我应该如何修改它们以防止出现此错误。我检查了其他类似的线程,但我正在使用等待并继续。那么这个错误是怎么发生的呢?

A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.

    var CrawlPage = Task.Factory.StartNew(() =>
{
return crawlPage(srNewCrawledUrl, srNewCrawledPageId, srMainSiteId);
});

var GetLinks = CrawlPage.ContinueWith(resultTask =>
{
if (CrawlPage.Result == null)
{
return null;
}
else
{
return ReturnLinks(CrawlPage.Result, srNewCrawledUrl, srNewCrawledPageId, srMainSiteId);
}
});

var InsertMainLinks = GetLinks.ContinueWith(resultTask =>
{
if (GetLinks.Result == null)
{

}
else
{
instertLinksDatabase(srMainSiteURL, srMainSiteId, GetLinks.Result, srNewCrawledPageId, irCrawlDepth.ToString());
}

});

InsertMainLinks.Wait();
InsertMainLinks.Dispose();

最佳答案

您没有处理任何异常。

改变这一行:

InsertMainLinks.Wait();

收件人:

try { 
InsertMainLinks.Wait();
}
catch (AggregateException ae) {
/* Do what you will */
}

一般来说:为了防止终结器重新抛出源自您的工作线程的任何未处理的异常,您可以:

等待线程并捕获 System.AggregateException,或者只读取异常属性。

如:

Task.Factory.StartNew((s) => {      
throw new Exception("ooga booga");
}, TaskCreationOptions.None).ContinueWith((Task previous) => {
var e=previous.Exception;
// Do what you will with non-null exception
});

Task.Factory.StartNew((s) => {      
throw new Exception("ooga booga");
}, TaskCreationOptions.None).ContinueWith((Task previous) => {
try {
previous.Wait();
}
catch (System.AggregateException ae) {
// Do what you will
}
});

关于c# - 通过等待任务或访问其 Exception 属性未观察到任务的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9193548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com