C# 退出 using() 块,且线程仍在作用域对象上运行

发布于 2024-07-25 01:36:01 字数 417 浏览 4 评论 0原文

如果线程正在运行通过退出 using 块而释放的对象中的方法,会发生什么情况?
示例:

    using (SomeObject obj = new SomeObject ())
    {
      obj.param = 10 ;
      Thread newThread = new Thread(() => { obj.Work(); });
      newThread.Start();
    }
    ... 
 

obj.Work() 在新线程上运行,但 obj 是一个 IDisposable 对象,通常会在 using 块退出时释放。 如果 using 块结束后线程继续运行会发生什么? 只有在线程完成后对象才会被释放吗? 还是线会断?

谢谢。

What happens to a thread if it is running a method in an object that was freed by exiting a using block?
Example:

    using (SomeObject obj = new SomeObject ())
    {
      obj.param = 10 ;
      Thread newThread = new Thread(() => { obj.Work(); });
      newThread.Start();
    }
    ... 
 

obj.Work() is running on a new thread but obj is an IDisposable object that would normally get released when the using block exits. What happens if the thread continues running after the using block ends? Will the object get disposed only after the thread completes? Or will the thread break?

Thanks.

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

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

发布评论

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

评论(4

澜川若宁 2024-08-01 01:36:01

有趣的事情将会发生。

具体来说,将在调用 Work 之前或之后调用 SomeObject 上的 dispose 方法,因为此时 Work 可能已计划运行,也可能未计划运行。

之后就看SomeObject的dispose方法做了什么; 例如,如果它释放了“工作”中未使用的 SqlConnection,那么就不应该有问题; 但是,如果 SomeObject 期望它尚未被释放,则该线程中可能会抛出异常。

Interesting things will happen.

Specifically, the dispose method on SomeObject will be called, either before or after Work has been called as it may or may not have been scheduled to run by that point.

After that, it depends on what the dispose method of SomeObject does; if it, say, releases a SqlConnection that isn't used in 'Work', then there shouldn't be an issue; if however SomeObject expects that it hasn't been disposed, you'll probably have an exception thrown in that thread.

誰ツ都不明白 2024-08-01 01:36:01

请记住,IDisposable 只是一种模式,不会释放与该对象关联的内存。 在这种情况下,using 块的关闭将调用 obj.Dispose,并且使用 obj 的另一个线程将继续运行。

这会给您带来奇怪的问题,因为 obj 的状态可能会在另一个线程使用它时发生更改(这完全取决于 Dispose 方法的实现方式)。 不用说,IDisposable、线程和 using 语句的这种应用充其量也会有问题。

Remember that IDisposable is just a pattern and doesn't free the memory associated with the object. This being the case the close of the using block will call obj.Dispose and the other thread that is using obj will continue to run.

This will create strange issues for you since obj's state may be changed while the other thread is using it (it all depends on how the Dispose method is implemented). Needless to say this application of IDisposable, threading, and the using statement will be problematic at best.

稀香 2024-08-01 01:36:01

该对象将在块末尾调用 Dispose。 它将继续运行,但 obj 会变得不稳定,因为 Dispose 应该关闭连接等。现在 obj 可能会被设置为检查某些东西是否正在使用并随后关闭它,但我不会指望这一点,除非你已经编写了对象来处理这个问题。

The object will call Dispose at the end of the block. It will continue to run, but obj will become unstable as Dispose is suppose to close connections etc. Now it is possible that obj would be setup to check if something is in use and close it afterwards, but I wouldn't count on this unless you've written the object to handle this.

快乐很简单 2024-08-01 01:36:01

当 using 块在主线程上退出时,它将 .Dispose() 对象,这可能会导致各种有趣的并发问题。 但是,该对象不会被垃圾收集——它将保留下来,但处于无效状态,具体取决于您对 .Dispose() 的实现。

When the using block is exited on the main thread, it will .Dispose() the object, which could cause all sorts of fun concurrency problems. However, the object won't be garbage collected--it will remain, but in an invalid state, depending on your implementation of .Dispose().

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