limit applications to one instance in Visual C++ .NET

G

Guest

Hello,
I have developped an application used by Windows Forms (.NET) at Visual C++
..NET v2003. I would like to limit my application to one instance working at a
moment. More than one working application is not allowed. I am requesting
help about this question.
I found 2 article at KnowledgeBase, which are Article ID : 243953 (How to
limit 32-bit applications to one instance in Visual C++) and Article ID :
238100 - (How to limit 32-Bit MFC SDI applications to a single instance on
WinCE). Unfortunately, these two articles are not fitted for my purpose and
required because of Windows Forms (.NET).

Thank you very much in advance.
--
~~~~~~~~~~~~~~~~~~~
İyi Çalışmalar
Alper AKÇAYÖZ (Bil Muh)

Wish You Good Work
Alper AKCAYOZ (Bil Muh)
 
R

Ronald Laeremans [MSFT]

Alper said:
Hello,
I have developped an application used by Windows Forms (.NET) at Visual C++
..NET v2003. I would like to limit my application to one instance working at a
moment. More than one working application is not allowed. I am requesting
help about this question.
I found 2 article at KnowledgeBase, which are Article ID : 243953 (How to
limit 32-bit applications to one instance in Visual C++) and Article ID :
238100 - (How to limit 32-Bit MFC SDI applications to a single instance on
WinCE). Unfortunately, these two articles are not fitted for my purpose and
required because of Windows Forms (.NET).

Thank you very much in advance.

The cannonical way is to create a named mutex or other named system
object in the startup code and check for a pr-existing one and exit if
there is one.

Ronald Laeremans
Visual C++ team
 
G

Guest

Hello,
I created a new Windows Forms .NET project. Then into the Form1 class in
Form1.H , I defined a public-signatured static Mutex object as below:
....
public __gc class Form1 : public System::Windows::Forms::Form
{
public: static System::Threading::Mutex *mt = NULL;
...
}

Then, into the main function of application that starts the program, I coded
blow statements:

#include "stdafx.h"
#include "Form1.h"
using namespace RunOnlyOnceAtATime;

[System::STAThreadAttribute]
void __stdcall _tWinMain()
{
Form1::mt = new System::Threading::Mutex(false, "myMutex");
if (!Form1::mt->WaitOne(10, true))
{
// Another copy of program is running
MessageBox::Show(S"Program is already running.", S"Working Program");
return;
}
else
{
//Run application.
Form1::mt->WaitOne();//Yalnızca bu çalışıyor mutexi kilitliyelim
Application::Run(new Form1());
}
}


Above codes solved my problem. Thank you very much.
--
~~~~~~~~~~~~~~~~~~~
İyi Çalışmalar
Alper AKÇAYÖZ (Bil Muh)

Wish You Good Work
Alper AKCAYOZ (Bil Muh)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top