我有这样的对象 preloadClient
它实现的IDisposable
,我要处理它,但经过异步方法完成他们的要求......这是不会发生
私人无效preLOAD(SlideHandler幻灯片)
{
使用(preloadClient客户端=新的preloadClient())
{
客户端preloadCompleted + = client_ preloadCompleted。
。客户端preLOAD(幻灯片);
}
//这里的客户端布置在紧靠
}
私人无效client_ preloadCompleted(对象发件人,幻灯preloadCompletedEventArgs E)
{
//此方法是经过一段时间被调用,
//但错误抛出试图访问对象时的状态(字段,属性)
}
因此,任何想法或变通??
解决方案您不应使用使用
结构,而是处理你的对象时,他们不再需要的:
//保持强引用列表,以避免垃圾收集,
//和处置他们都在的情况下,我们正在处理的封装对象
私人只读表< preloadClient> _activeClients =新的名单,其中,preloadClient>();
私人无效preLOAD(SlideHandler幻灯片)
{
preloadClient客户端=新的preloadClient();
_activeClients.Add(客户端);
客户端preloadCompleted + = client_ preloadCompleted。
。客户端preLOAD(幻灯片);
}
私人无效client_ preloadCompleted(对象发件人,
幻灯preloadCompletedEventArgs E)
{
preloadClient客户端=发件人为preloadClient;
//做的东西
。客户端preloadCompleted - = client_ preloadCompleted;
client.Dispose();
_activeClients.Remove(客户端);
}
在这种情况下,你必须在处理主类,以处理所有的客户端:
保护覆盖的Dispose(BOOL处置)
{
的foreach(在_activeClients preloadClient客户端)
{
。客户端preloadCompleted - = client_ preloadCompleted;
client.Dispose();
}
_activeClients.Clear();
base.Dispose(处置);
}
注意,此实现不是线程安全的
访问 _activeClients
列表必须是的线程安全,作为你的 preloadCompleted
方法被调用从不同的线程
您的包含对象可以是客户端在事件之前处理。在这种情况下,做的东西应该什么都不做,所以这是另外一件事,你应该照顾。
这可能是使用一个好主意尝试
/ 最后
事件处理程序内部的块,以确保该对象被设置在所有情况下
I have this object PreloadClient
which implements IDisposable
, I want to dispose it, but after the asynchronous methods finish their call... which is not happening
private void Preload(SlideHandler slide)
{
using(PreloadClient client = new PreloadClient())
{
client.PreloadCompleted += client_PreloadCompleted;
client.Preload(slide);
}
// Here client is disposed immediately
}
private void client_PreloadCompleted(object sender, SlidePreloadCompletedEventArgs e)
{
// this is method is called after a while,
// but errors are thrown when trying to access object state (fields, properties)
}
So, any ideas or work arounds ??
解决方案You shouldn't use the using
construct, but rather dispose your objects when they are no longer needed:
// keep a list of strong references to avoid garbage collection,
// and dispose them all in case we're disposing the encapsulating object
private readonly List<PreloadClient> _activeClients = new List<PreloadClient>();
private void Preload(SlideHandler slide)
{
PreloadClient client = new PreloadClient();
_activeClients.Add(client);
client.PreloadCompleted += client_PreloadCompleted;
client.Preload(slide);
}
private void client_PreloadCompleted(object sender,
SlidePreloadCompletedEventArgs e)
{
PreloadClient client = sender as PreloadClient;
// do stuff
client.PreloadCompleted -= client_PreloadCompleted;
client.Dispose();
_activeClients.Remove(client);
}
in this case, you have to dispose all clients when disposing the main class:
protected override Dispose(bool disposing)
{
foreach (PreloadClient client in _activeClients)
{
client.PreloadCompleted -= client_PreloadCompleted;
client.Dispose();
}
_activeClients.Clear();
base.Dispose(disposing);
}
Note that this implementation is not thread safe
Access to the_activeClients
list must be made thread-safe, as your PreloadCompleted
method is called from a different thread
Your containing object may be disposed before a client fires the event. In that case "do stuff" should do nothing, so this is another thing you should take care of.
It might be a good idea to use a try
/finally
block inside your event handler, to make sure that the object gets disposed in all cases