用 using(...) { } 语句包装 Application.Run(new Form()) 是否安全?
我正在使用外部 API 来连接 FireWire 相机。该 API 可能是用 C++ 编写的,但幸运的是它带来了自己的 .NET 包装器 DLL。该 API 需要以下过程:
ApiResource.Init();
// ... use the ressource here ...
ApiResource.CloseAll();
ApiResource.Release();
因为我需要一些特定的处理代码,所以我决定为此编写一个包装类。由于事件处理程序等原因,我需要在表单打开时保持资源打开。因此,我想使包装器更易于使用,我将使其成为一个实现 IDisposable
的单例,以便我可以包装它在 using
语句中。我想要 Singleton 的原因是有一种受控且有限的方式来调用所需的 API 函数:
class Wrapper : IDisposable {
private Wrapper _instance;
public Wrapper Instance
{
get
{
if(_instance == null)
_instance = new Wrapper();
return _instance;
}
}
private Wrapper ()
{
ApiResource.Init();
_disposed = false;
}
// Finalizer is here just in case someone
// forgets to call Dispose()
~Wrapper() {
Dispose(false);
}
private bool _disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(!_disposed)
{
if(disposing)
{
}
ApiResource.CloseAll();
ApiResource.Release();
_instance = null;
log("Wrapper disposed.");
_disposed = true;
}
}
}
我想要使用它的方式是这样的:
using(Wrapper.Instance) {
Application.Run(new Form());
}
对 C# 很陌生,所以我对以下几件事非常不确定:
- 我
Dispose()
总是在上面using(Singleton.Instance) { ... }
中调用?我的日志显示“是”,但我不确定... - 用
using
语句包装Application.Run(...)
是否安全?
I am using an external API to interface to a FireWire Camera. The API is propably written in C++ but thankfully it brings its own .NET wrapper DLLs. The API requires the following procedure:
ApiResource.Init();
// ... use the ressource here ...
ApiResource.CloseAll();
ApiResource.Release();
Because I need some specific handling code I decided to write a wrapper class for this. I need to keep the ressources open while my Form is open because of Event Handlers, etc. So I thought to make the wrapper easier to use I'd make it a singleton implementing IDisposable
so I can wrap it in a using
statement. The reason I want a Singleton is to have a controlled and limited way of calling the required API functions:
class Wrapper : IDisposable {
private Wrapper _instance;
public Wrapper Instance
{
get
{
if(_instance == null)
_instance = new Wrapper();
return _instance;
}
}
private Wrapper ()
{
ApiResource.Init();
_disposed = false;
}
// Finalizer is here just in case someone
// forgets to call Dispose()
~Wrapper() {
Dispose(false);
}
private bool _disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(!_disposed)
{
if(disposing)
{
}
ApiResource.CloseAll();
ApiResource.Release();
_instance = null;
log("Wrapper disposed.");
_disposed = true;
}
}
}
The way I want to use it is this:
using(Wrapper.Instance) {
Application.Run(new Form());
}
I'm quite new to C# so I am very unsure of a couple of things:
- Will
Dispose()
always be called in the aboveusing(Singleton.Instance) { ... }
? My logging suggests "yes", but I'm unsure... - Is it safe to wrap
Application.Run(...)
with ausing
-statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的两个问题的答案都是是:
using
块结束时,Dispose()
将始终被调用,除非Wrapper.Instance 为
null
。将对
Run()
的调用包装在using
块中是完全安全的。The answer to both of your questions is yes:
Dispose()
will always be called when theusing
block ends, unlessWrapper.Instance
wasnull
when the block began.It is perfectly safe to wrap the call to
Run()
in ausing
block.