本文介绍了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.

推荐答案

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();


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单实例使用互斥锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 04:29
查看更多