TransactionScope 和超时问题 [英] TransactionScope and Timeout Issue

查看:33
本文介绍了TransactionScope 和超时问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道 TransactionScope 类可以使用用户定义的超时值.但是从 using {} 块退出时会抛出超时异常.如何在经过超时值后立即抛出此超时异常?

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?

推荐答案

这是不可能的.

TransactionScope 只是存储您开始事务的时间,然后在提交事务时检查该时间.
它无法在任意点抛出异常.

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.

一般来说,在(托管代码的)执行过程中任何点可以抛出的唯一异常是ThreadAbortException.

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.

这篇关于TransactionScope 和超时问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆