Winform单实例使用互斥锁 [英] Winform Single Instance using mutex

查看:114
本文介绍了Winform单实例使用互斥锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个winform应用程序,可以打开其他winform对象.我需要一种将这些其他winform对象的实例数限制为一个的方法.我了解互斥.但是我不知道如何将winform对象覆盖到一个实例.注意,我并不是在问应用程序本身.

I have a winform application that opens other winform objects. I need a way to limiting the number of instances of these other winform objects to one. I understand mutexs. But I don''t know how to override the winform object to one instance. Note I am not asking about the applications itself.

推荐答案

您确定要在C ++/CLI中使用它吗?如果是这样,这是一个使用C ++/CLI语法的单例表单实现:

Are you sure you want this in C++/CLI? If so, here''s a singleton form implementation that uses C++/CLI syntax:

public ref class SingletonForm : public System::Windows::Forms::Form
{
public:
  static property SingletonForm^ Instance
  {
    SingletonForm^ get()
    {
      return instance == nullptr ? instance = gcnew SingletonForm() : instance;
    }
  }

private:
  SingletonForm(void)
  {
    InitializeComponent();
  }

protected:
  ~SingletonForm()
  {
    if (components)
    {
      delete components;
    }
  }

private:
  static SingletonForm^ instance;
  System::ComponentModel::Container ^components;
}



您将因此使用它:



And you would use it thus:

SingletonForm::Instance->Show();


我认为这是您的操作方式.

I think this is how you''d do it.

public partial class CSingletonForm : Form
{
    public:
        static CSingletonForm& Instance()
        {
            static CSingletonForm singleton;
            return singleton;
        }

    // Other non-static member functions
    private:
        CSingletonForm() {};                                   // Private constructor
        CSingletonForm(const CSingletonForm&);                 // Prevent copy-construction
        CSingletonForm& operator=(const CSingletonForm&);      // Prevent assignment
};



然后从该对象继承.



And then inherit from that object.


这篇关于Winform单实例使用互斥锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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