c# - Call a method that has already been called in a new thread -
i have method "importexcel" called in new thread:
[stathread] private void btnimportexcel_click(object sender, eventargs e) { // start setting new thread using delegate threadstart // tell entry function (the function call first in thread) // think of main() new thread. threadstart theprogress = new threadstart(importexcel); // thread create using delegate thread startprogress = new thread(theprogress); startprogress.setapartmentstate(apartmentstate.sta); // can give name (optional) startprogress.name = "book detail scrapper"; // start execution startprogress.start(); }
now within importexcel() function, there try catch block. in catch block, if particular exception occurs, wish call importexcel() function again. how do that?
probably add 1 more level of indirection handle such issue:
private void trymultimpleimportexcel() { boolean cantryagain = true; while( cantryagain) { try { importexcel(); cantryagain = false; } catch(notcriticaltryagainexception exc) { logger.log(exc); } catch(exception critexc) { cantryagain = false; } } } // ... threadstart theprogress = new threadstart(trymultimpleimportexcel); // .. startprogress.start();
also:
if want allow user stop possibly endless processing, may want use cancellationtoken, described here - how use cancellationtoken property?. @mikeofsst.
Comments
Post a Comment