TransactionScope and Timeout Issue

2019-07-24 18:26发布

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条回答
霸刀☆藐视天下
2楼-- · 2019-07-24 19:30

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.

查看更多
登录 后发表回答