Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

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

MC++
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:

MC++
SingletonForm::Instance->Show();
 
Share this answer
 
Comments
Espen Harlinn 24-Feb-11 19:37pm    
Good reply, my 5
I think this is how you'd do it.

C++
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.
 
Share this answer
 
Comments
Nish Nishant 24-Feb-11 11:35am    
John, that syntax is a mix of C# and C++ but it's not valid C++/CLI :-)
#include "main_form.h"

using namespace System;
using namespace System::Windows::Forms;
using System::Threading::Mutex;

[STAThread]
int main(array<String^>^ args)
{
	// Set this variable to false if you do not want to request 
	// initial ownership of the named mutex.
	bool requestInitialOwnership = true;
	bool mutexWasCreated = false;

	// Request initial ownership of the named mutex by passing
    // true for the first parameter. Only one system object named 
    // "MutexGUI" can exist; the local Mutex object represents 
    // this system object. If "MutexGUI" is created by this call,
    // then mutexWasCreated contains true; otherwise, it contains
    // false.
	Mutex^ myMutex = gcnew Mutex(requestInitialOwnership, "MutexGUI", mutexWasCreated);

	// This thread owns the mutex only if it both requested 
    // initial ownership and created the named mutex. Otherwise,
    // it can request the named mutex by calling WaitOne.
	if (!(requestInitialOwnership && mutexWasCreated))
	{
		MessageBox::Show("The application is open.", "Application ", MessageBoxButtons::OK, MessageBoxIcon::Information);

		/* Releases all resources held by the current WaitHandle element */
		myMutex->Close();
	}
	else
	{
		/*  Enabling Windows visual effects before any controls are created */
		Application::EnableVisualStyles();
		Application::SetCompatibleTextRenderingDefault(false);

		/* Create the main window and run it */
		Application::Run(gcnew NAME_PROJECT::main_form());

		myMutex->ReleaseMutex();
	}
	
	return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900