.net - Using TransactionScope to time out regular (non db related) c# code -


i trying use transactionscope class put specific timeout code execution. more specifically,

static void main(string[] args)     {         int counter = 0;         try         {             using (var scope = new transactionscope(transactionscopeoption.required, new timespan(0, 0, 5)))                             {                 (int = 0; < 50000; i++)                 {                     console.writeline("counter : {0}", counter);                     counter++;                     thread.sleep(100);                 }                  scope.complete();             }         }         catch (exception ex)         {              console.writeline("exception : {0}", ex);         }     } 

after reading transactionscope , expect above code block in using statement aborted in 5 seconds.

but not behave so. loop continues proceed irrespective of transaction timeout (which did not expect). execution of loop ends when = 50000 , below exception after using statement ends:

exception : system.transactions.transactionabortedexception: transaction has aborted. ---> system.timeoutexception: transaction timeout    --- end of inner exception stack trace ---    @ system.transactions.transactionstateaborted.begincommit(internaltransaction tx, boolean asynccommit, asynccallback asynccallback, object asyncstate)    @ system.transactions.committabletransaction.commit()    @ system.transactions.transactionscope.internaldispose()    @ system.transactions.transactionscope.dispose() 

am doing sth wrong? transactionscope class can used db related code?

what actualy need kind of mechanism must set timeout block of code execution. found several links:

how set timeout line of c# code

set timeout operation

implement c# generic timeout

they seem execute timeout requiring code in seperate thread , abort thread when timeout occurs. think messy , not want because of cost of thread creation , problems of thread abort explained here :

http://blogs.msmvps.com/peterritchie/2007/08/22/thead-abort-is-a-sign-of-a-poorly-designed-program/

i thought use transaction scope timeout any code block not behave that.

what best solution put time limit code block execution?

far know, unless code block written know can time out (for instance, it's loop checking elapsed time , termitating if necessary), there no way timeout operation without aborting thread. messy, you've pointed out. if code-block limitations permit (no state), start separate process (have pool of them @ disposal, 'cause that's way more expensive creating threads) ensure code block won't affect anything, , run code there. kill process, if code won't return in time. down path, you'll have come way of invoking code, assembly resolution, etc. possibly, use sandboxing solutions... (might use appdomains instead of processes, can bring host process down if things go south).

i don't think transactionscope candidate such job.


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -