-
October 12th, 2006, 08:34 PM
#1
Having single instance application
Hello guys,
I have a Winforms application written in managed C++. I would like to only have one instance of my application running on a given system at a time. The reason being opening another instance of my form can really mess up the environment I'm working in.
Here's what I'm using -
public ref class CSingleInstance
{
private:
//our Mutex member variable
Mutex ^m_mutex;
public:
CSingleInstance(String ^mutexname)
{
m_mutex = gcnew Mutex(false, mutexname);
}
~CSingleInstance()
{
//release it when the CSingleInstance object is destroyed
m_mutex->ReleaseMutex ();
}
bool IsRunning()
{
return !m_mutex->WaitOne(5, true);
}
};
And in my main() -
//create a mutex with a unique name
CSingleInstance ^si= gcnew CSingleInstance("{94374E65-7166-4fde-ABBD-4E943E70E8E8}");
if(si->IsRunning())
MessageBox::Show("App already running!!");
else
// Create the main window and run it
Application::Run(gcnew Form1("NULL", "NULL"));
But the problem I'm running into is that, this only works on my laptop. When i install my application on another system, it doesnt work.
Can anybody help please??
-
October 12th, 2006, 11:33 PM
#2
Re: Having single instance application
Please use Code tag First.Second thing Simply use CreateMutex() Function .The
CreateMutex function creates or opens a named or unnamed mutex object.If the function succeeds, the return value is a handle to the mutex object. If the named mutex object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS. Otherwise, the caller created the mutex.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
for More Detail have a look in MSDN
Thanx
-
October 13th, 2006, 12:20 AM
#3
Re: Having single instance application
Here is the code sample..
Code:
HANDLE hndle = CreateMutex(NULL,FALSE,"TestMutex");
if(GetLastError() != ERROR_ALREADY_EXISTS)
{
//..Do your task
}
else
{
//..One Instance is already running
return;
}
-
October 13th, 2006, 12:47 AM
#4
Re: Having single instance application
-
October 13th, 2006, 01:06 AM
#5
Re: Having single instance application
People we are talking here about a managed application, not Win32 API.
Well, this runs ok on my PC. At the first running, exists is true, at the second (if started in less than 5 seconds) is false, just as expected.
Code:
using namespace System;
using namespace System::Threading;
int main(array<System::String ^> ^args)
{
Mutex^ m = gcnew Mutex( false,"MyMutex" );
bool exists = m->WaitOne(10, true);
Console::WriteLine("Application already running {0}", exists);
Thread::Sleep(5000);
return 0;
}
-
October 13th, 2006, 12:19 PM
#6
Re: Having single instance application
I figured out that there is one important thing we need to add -
GC::KeepAlive(mutex) to keep a hold of the mutex as long as the program is running. Now it works perfect.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|