用 using(...) { } 语句包装 Application.Run(new Form()) 是否安全?

发布于 2024-11-24 10:26:52 字数 1560 浏览 2 评论 0原文

我正在使用外部 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# 很陌生,所以我对以下几件事非常不确定:

  1. Dispose() 总是在上面 using(Singleton.Instance) { ... } 中调用?我的日志显示“是”,但我不确定...
  2. 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:

  1. Will Dispose() always be called in the above using(Singleton.Instance) { ... }? My logging suggests "yes", but I'm unsure...
  2. Is it safe to wrap Application.Run(...) with a using-statement?

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

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

发布评论

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

评论(1

浪推晚风 2024-12-01 10:26:52

你的两个问题的答案都是

  • using 块结束时,

    Dispose() 将始终被调用,除非Wrapper.Instance 为 null

  • 将对 Run() 的调用包装在 using 块中是完全安全的。

The answer to both of your questions is yes:

  • Dispose() will always be called when the using block ends, unless Wrapper.Instance was null when the block began.

  • It is perfectly safe to wrap the call to Run() in a using block.

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