TransactionScope 和超时问题

发布于 2024-09-11 13:45:52 字数 85 浏览 2 评论 0原文

我们知道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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2024-09-18 13:45:52

这是不可能的。

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

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

因此,如果您确实愿意,您可以创建一个单独的线程,在超时期间休​​眠,然后中止原始线程。
然而,这是一个可怕的想法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文