views:

37

answers:

1

Hi,

We know that TransactionScope class can use user-defined timeout value. But timeout exception is thrown while exiting from the using {} block. How to throw this timeoutexception immediately after elapsed timeout value?

+1  A: 

This is not possible.

The TransactionScope simply stores the time that you started the transaction, then checks that time when committing the transaction.
It has no way to throw an exception at any arbitrary point.

In general, the only exception that can be thrown at any point in execution (of managed code) is ThreadAbortException.

Therefore, if you really wanted to, you could make a separate thread that sleeps for the duration of the timeout, then aborts your original thread.
However, that's a horrible idea.

SLaks
why does your solution horrible? it looks so good. I implemented and it works very well. Thank you so much.
mkus
Thread aborts can interrupt code anywhere and mess up shared state.
SLaks
http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation
SLaks