4

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.

    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();
svick
  • 236,525
  • 50
  • 385
  • 514
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • Hmm, nothing obviously wrong. Have you tried running in the debugger to see if you can figure out which task is getting leaked? (And btw, you've got some strange coding conventions here: you're using `PascalCase` for your local vars and `camelCase` for your methods, which is backwards from usual .NET conventions.) – Joe White Feb 08 '12 at 13:09
  • Thanks for answer. Then probably something else is incorrect in the code. I will check :) – Furkan Gözükara Feb 08 '12 at 13:30
  • 1
    You can get this error if an unhandled exception is thrown while the task is executing. Have you tried enclosing the call to ReturnLinks and insertLinksDatabase in a try/catch block? – Josh Feb 08 '12 at 13:46
  • Thanks for answer Josh. I suppose unexpected errors happened there should not make the application crash since they are also being called in another task. They should be handled since they are being called in another task and handled via ContinueWith. Right now trying something else. If error happens again i will try your suggestion. – Furkan Gözükara Feb 08 '12 at 15:08

1 Answers1

6

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.

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
    }
});
Riko
  • 562
  • 5
  • 11