May 21, 2024, 03:53:39 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Single Instance Applications

Started by Zen, July 13, 2006, 05:34:11 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zen

I was just wondering how i would prevent an application i am working on being opened more than once at the same time. i.e. I can make it display a message if the user tries to start another instance of the application. Like MSN or Ssteam.

Lewis

Protected

I think there's some way to identify the tasks running on the windows side, by assigning a 'class name' to them. So you could check if that class name was present in the existing tasks before creating your window. You can also use the handle of the dialog. Or you can keep the status of your application manually, for example, in a file status.dat in the application's directory: When the application opens you read the value from the file and check if it's true. If it is, then your application is already running. You do whatever cleaning up you want and exit. If it's not, you write 'true' in the file and carry on executing the program. Then, before leaving, you write 'false' in the file. The problem with this is that while exception handling is not implemented I don't think it's possible to prevent the program from locking itself when there's an error that forces the program to end without setting the value back to 'false'...

Zen

Hmmm. I've not really looked into DDE but maybe that is a way to go, as Aurora uses DDE to open files in the lready open window rather than start  new instance of the IDE. Although this does not stop the user from opening another instance of the application, perhaps this is the right way to go.

I will look into the windows API over tomorrow.

Lewis

Zen

Ok i have just seen a simple solution on the code project website which says to attempt to create a mutex every time an instance of the application is started, if this fails we know there is already an instance running.

Sounds simple enough to me, ill give it a go and post some code later incase anyone else is interested.

Lewis

Protected

Can you use the same mutex in different processes?

Ionic Wind Support Team

That will work.  As long as it is a named mutex.
Ionic Wind Support Team

Parker

July 13, 2006, 07:24:09 PM #6 Last Edit: July 13, 2006, 07:26:54 PM by Parker
I did this in IBasic a while ago. This should give you the idea (pseudo code, wouldn't actually compile):
main( ) {
    global hMutex = CreateMutex( null, true, "MyUniqueMutexName" )
    if( GetLastError( ) == ERROR_ALREADY_EXISTS ) {
        ShowMessage( "I am already running" )
        CloseHandle( global hMutex )
        return;
    }
}

on AppExit( ) {
    CloseHandle( global hMutex )
}

Edit - a mutex is a system-wide handle. According to MSDN, if the mutex already exists GetLastError will return ERROR_ALREADY_EXISTS. CreateMutex always succeeds unless there is some other error like out of memory or access denied.

Rock Ridge Farm (Larry)

A simple minded way would be to use a lock file.
Check for the existance of the lock file at startup.
Problem would be that you would need a way to clean up an old lock file in case of crashes.

Zen

Larry, that was my first idea, but like you said, if the program crashes, not only will you be able to then open further instances which would be intended but you wouldnt even be able to open the first instance, and as i intend this application to go commercial, asking users to delete a file everytime it crashes is a bit tedious. Using the named mutex is the way i haave gone about it.

Thanks for the feedback everyone.

Lewis

Parker

Plus a file is available for anyone to modify. A mutex is much more professional ;)