c# - Managing thread exception using a Task -


i'm creating task in main thread in order manage thread. purpose benefit aggregateexception, want task return exception once exception occurs in thread started in task shown in codeblock below.

public void reportgeneratorfivethreadtest() {     var logger = new log4netlogger(typeof(reportgeneratorthreadtest));     var tasks = new list<task>();      (var = 0; < 1; i++)     {         var estimatedreportsize = estimatedreportsize.normal;         var thread = new reportgeneratorthread(logger, new reportgenerator(20), estimatedreportsize);         var task = task.factory.startnew(         () =>         {             thread.start();                             });         tasks.add(task);     }      try     {        task.waitall(tasks.toarray());        debug.writeline("tasks completed");     }     catch (aggregateexception e)     {         foreach (var v in e.innerexceptions)         {             debug.writeline(e.message + " " + v.message);         }     } }  

generatereport() method called started thread , throws exception defined in method.

public void generatereport() {     var didwork = false;     try     {         didwork = this.reportgenerator.generatereport(this.estimatedreportsize);     }     catch (exception e)     {         this.log.logerror(reportgenerator.correlationidforpickingreport, string.format(cultureinfo.invariantculture, "system"), string.format(cultureinfo.invariantculture, "error during report generation."), 0, e);     }         {         if (!didwork)         {             thread.sleep(settings.default.reportgenerationinterval);         }     } } 

the problem exception not presented in task thus, aggregateexception not take place in case. possible present innerexception, occurs in thread in task, aggregateexception triggered ?.

update----------------------

i have modified code following:

 public void start()  {         this.running = true;         this.t = task.run(         () =>         {             this.generatereport();         });         try         {             task.waitany(this.t);         }         catch (aggregateexception e)         {             foreach (var v in e.innerexceptions)             {                 debug.writeline(e.message + " " + v.message);             }         }  } 

and with:

  public void generatereport()     {         var didwork = false;                     try         {             didwork = this.reportgenerator.generatereport(this.estimatedreportsize);                         }         catch (exception e)         {             this.log.logerror(reportgenerator.correlationidforpickingreport, string.format(cultureinfo.invariantculture, "system"), string.format(cultureinfo.invariantculture, "error during report generation."), 0, e);             throw new invalidoperationexception("failed");         }                 {             if (!didwork)             {                 thread.sleep(settings.default.reportgenerationinterval);                 throw new invalidoperationexception("failed");             }         }     } 

and still not catch aggregateexception though invalidoperationexception thrown in generatereport() method.

firstly must rethrow exception in catch block of generatereport method

public void generatereport() {     var didwork = false;     try     {         ...     }     catch (exception e)     {         this.log.logerror(...);         throw; // important!     }         {         if (!didwork)         {             ...         }     } } 

secondly shouldn't use thread inside task. use tasks

public void reportgeneratorfivethreadtest() {     var tasks = new list<task>();      (var = 0; < 1; i++)     {         var task = task.factory.startnew(() =>         {             // no need thread here             generatereport();         });         tasks.add(task);     }      try     {         ...     }     catch (aggregateexception e)     {         ...     } } 

Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

html - Outlook 2010 Anchor (url/address/link) -