可以锁定处置? [英] Can lock into a dispose?

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

问题描述

我有一个实现IDisposable接口的A类,并且有一个方法

启动一个线程。

在Dispose(bool)中我可以使用lock关键字等待线程退出之前

处理它使用的资源,或者从消费者处理

类型为A的对象时足够注意课程?

谢谢,

Luigi。

I''ve a class A that implements IDisposable interface and has a method that
start a thread.
In Dispose(bool) can I use the lock keyword to wait for thread exit before
disposing resources it use or it''s enough to pay attention when disposing
objects of type A from "consumer" classes?
Thanks,
Luigi.

推荐答案

Luigi,


在终结器中不鼓励使用锁定:

http://blogs.msdn.com/yunjin/archive...05/435726.aspx


最重要的是,如果你花了太长时间,如果它是应用程序域的结果,你的

终结器将会被关闭的可能性

关闭下来:

http://www.bluebytesoftware.com/blog...600869018.aspx


我建议你有一个方法,你打电话给等为了终止

线程,然后处理你的对象。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" BLUE" < bluewrote in message news:OT ************** @ TK2MSFTNGP04.phx.gbl ...
Luigi,

The use of lock is discouraged in a finalizer:

http://blogs.msdn.com/yunjin/archive...05/435726.aspx

On top of that, if you take too long, the possibility exists that your
finalizer will be shut down anyways if it is the result of an app domain
shutting down:

http://www.bluebytesoftware.com/blog...600869018.aspx

I would recommend that you have a method that you call to wait for the
thread to terminate, then dispose of your object.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"BLUE" <bluewrote in message news:OT**************@TK2MSFTNGP04.phx.gbl...

I''有一个实现IDisposable接口的类A,并且有一个方法,即
启动一个线程。

在Dispose(bool)中我可以使用lock关键字等待线程退出之前

处理它所使用的资源,或者在处理来自消费者的类型A的
对象时,它足以引起注意。课程?


谢谢,

Luigi。

I''ve a class A that implements IDisposable interface and has a method that
start a thread.
In Dispose(bool) can I use the lock keyword to wait for thread exit before
disposing resources it use or it''s enough to pay attention when disposing
objects of type A from "consumer" classes?
Thanks,
Luigi.


It 这是个大问题!


我的GUI有一个定时器,用于触发与WS的数据同步:当

计时器过去时,它调用一个BLL的方法又调用了DAL的同步方法。

同步创建一个线程,将与WS同步数据引发事件

表示同步过程的每一步(即使是错误)。


每一层(演示文稿除外)都是一个单例类,

实现IDisposable接口。


所以在表单的Closing事件中,也是在Dispose方法中我会

调用bll.Dispose()并在BLL.Dispose方法我会调用dal.Dispose()。

现在在计时器刚刚过去的情况下,用户决定关闭

应用程序我必须等待同步结束,但是如何?

我可以在结束事件中调用方法来进行di sable the timer我必须

等待最后一个事件(dataSynced)或错误事件但如何停止关闭

直到同步?

我假设我不能让用户等待很长时间,因为它不太好

我不能中断线程,因为我在WinCE 4.2上使用CF 1.x!

此外这个等待不能以Dispose形式完成所以它永远不会
处理dal!

感谢您的帮助!

Luigi。
It''s a big big problem!

My GUI has a timer that is used to trigger data sync with a WS: when the
timer elapses it calls a method of the BLL which in turn call Sync method of
the DAL.
Sync create a thread that will synchronize data with the WS raising an event
for each step of the sync process (even an error).

Each layer (except for the presentation one) is a singleton class that
implements IDisposable interface.

So in the Closing event of the form, but also in the Dispose method I''ll
call bll.Dispose() and in the BLL.Dispose method I''ll call dal.Dispose().
Now in the case the timer has just elapsed and the user decide to close the
app I must wait sync to end, but how?
I could call a method in the closing event to disable the timer and I must
wait the last event (dataSynced) or the error event but how to stop closing
until synchronization?
I suppose I cannot make the user wait for a long time since it''s not so good
and I cannot abort the thread since I''m using CF 1.x on WinCE 4.2!
Moreover this wait cannot be done in the form Dispose so it will never
dispose the dal!
Thank you for any help!
Luigi.


我很好奇,为什么你会有一个单独的类,

实现了IDispose?那些应该是实例,因为你暗示

他们有一个明确的生命周期,这有点与

单身人士相反。


在你要关闭的情况下,你将不得不采取某种机制来取消另一个线程上的

操作。你真的

不应该在Dispose方法中处理这个。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" BLUE" < bluewrote in message

news:u%****************** @ TK2MSFTNGP04.phx.gbl ...
I am curious, why are you going to have a singleton class that
implements IDispose? Those should be instances, because you are implying
that they have an explicit lifetime, which runs somewhat contrary to what a
singleton is.

You are going to have to make some sort of mechanism to cancel the
operation on the other thread in the case you want to shut down. You really
shouldn''t be handling this in the Dispose method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"BLUE" <bluewrote in message
news:u%******************@TK2MSFTNGP04.phx.gbl...

这是一个很大的问题!


我的GUI有一个定时器,用于触发与WS的数据同步:当/>
计时器过去它调用BLL的方法,后者又调用同步方法

的DAL。

同步创建一个将同步数据的线程WS为同步过程的每一步提出了一个

事件(即使是一个错误)。


每一层(演示文稿除外)都是单身

实现IDisposable接口的类。


所以在表单的Closing事件中,也是在Dispose方法中我会

调用bll.Dispose()并在BLL.Dispose方法中调用dal.Dispose()。

现在,在计时器刚刚过去且用户决定关闭的情况下

应用程序我必须等待同步结束,但是如何?

我可以调用方法我n关闭事件禁用计时器我必须

等待最后一个事件(dataSynced)或错误事件但如何停止

关闭直到同步?

我想我不能让用户等待很长时间因为它不是那么好b $ b好​​我不能中断线程因为我在WinCE上使用CF 1.x 4.2!

此外这个等待不能以Dispose形式完成所以它永远不会
处理dal!


谢谢任何帮助!

Luigi。
It''s a big big problem!

My GUI has a timer that is used to trigger data sync with a WS: when the
timer elapses it calls a method of the BLL which in turn call Sync method
of the DAL.
Sync create a thread that will synchronize data with the WS raising an
event for each step of the sync process (even an error).

Each layer (except for the presentation one) is a singleton class that
implements IDisposable interface.

So in the Closing event of the form, but also in the Dispose method I''ll
call bll.Dispose() and in the BLL.Dispose method I''ll call dal.Dispose().
Now in the case the timer has just elapsed and the user decide to close
the app I must wait sync to end, but how?
I could call a method in the closing event to disable the timer and I must
wait the last event (dataSynced) or the error event but how to stop
closing until synchronization?
I suppose I cannot make the user wait for a long time since it''s not so
good and I cannot abort the thread since I''m using CF 1.x on WinCE 4.2!
Moreover this wait cannot be done in the form Dispose so it will never
dispose the dal!
Thank you for any help!
Luigi.


这篇关于可以锁定处置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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