These are my tasks. How should i modify them to prevent this error. I checked the other similar threads but i am using wait and continue with. So how come this error happening?
这些是我的任务。我应该如何修改它们以防止这个错误。我检查了其他类似的线程,但是我正在使用等待和继续。为什么这个错误会发生呢?
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.
任务的异常(s)不是通过等待任务或访问其异常属性来观察的。因此,未观察到的异常会被终结器线程重新抛出。
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();
1 个解决方案
#1
5
You're not handling any exception.
你没有处理任何异常。
Change this line:
改变这条线:
InsertMainLinks.Wait();
TO:
:
try {
InsertMainLinks.Wait();
}
catch (AggregateException ae) {
/* Do what you will */
}
In general: to prevent the finalizer from re-throwing any unhandled exceptions originating in your worker thread, you can either:
通常:为了防止终结器重新抛出工作线程中产生的未处理的异常,您可以:
Wait on the thread and catch System.AggregateException, or just read the exception property.
在线程和catch系统上等待。AggregateException,或者只是读取异常属性。
EG:
例如:
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
});
OR
或
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
}
});
#1
5
You're not handling any exception.
你没有处理任何异常。
Change this line:
改变这条线:
InsertMainLinks.Wait();
TO:
:
try {
InsertMainLinks.Wait();
}
catch (AggregateException ae) {
/* Do what you will */
}
In general: to prevent the finalizer from re-throwing any unhandled exceptions originating in your worker thread, you can either:
通常:为了防止终结器重新抛出工作线程中产生的未处理的异常,您可以:
Wait on the thread and catch System.AggregateException, or just read the exception property.
在线程和catch系统上等待。AggregateException,或者只是读取异常属性。
EG:
例如:
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
});
OR
或
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
}
});