3

I have rewritten a VB6 application in Delphi. It should have only one instance running. How can I do this with minimum of code?

In VB6 we just have to use one single line of code >

If App.PrevInstance Then 'Take some action End If

On goggling I did find a solution but it is very length and we have to mess with .drp file.

I do not want to do that.

I want something simpler.

raven
  • 18,004
  • 16
  • 81
  • 112
Yogi Yang 007
  • 5,147
  • 10
  • 56
  • 77
  • 3
    Extended discussion of this topic can be found in answers to this: http://stackoverflow.com/questions/459554/how-do-i-tell-if-one-instance-of-my-program-is-running, including links to components that wrap the low-level code. – mghie Mar 12 '10 at 11:35
  • Yogi, if all you wish to know is *whether* a previous instance exists, then this question is a duplicate of the one Mghie linked to. But if you want to know additional information about that previous instance, then this question is *not* a duplicate. **Please clarify:** Your code sample suggests you just want to check existence, but the title of your question suggests you want to know more. – Rob Kennedy Mar 12 '10 at 14:45
  • Rob, I want to know more. I have read quite a few articles on this subject but the amount of coding that is required is just daunting for me. I want to understand and if possible make this as simple as it is in VB6! The code sample is just to give ideas as to what I have used in VB6 app. – Yogi Yang 007 Mar 15 '10 at 04:32

3 Answers3

3

I have some code along the lines of:

var
    AppMutex: THandle;

{ .... }


initialization
    // Create the mutex
    AppMutex := CreateMutex(nil, True, 'MY-APPLICATION-NAME');
    if (AppMutex = 0) or (GetLastError = ERROR_ALREADY_EXISTS) then
    begin
        MessageDlg('My application is already running on this computer.'#13#10+
            'You should close the other instance before starting a new one.',mtError,
            [mbOK],0);
        Halt;
    end;

finalization
    // Close the mutex
    CloseHandle(AppMutex);

but I'm sure the answers in the thread that @mghie linked to are more helpful/richer features!

Edit: Note you can make this into a small unit in it's own right, then just use that unit in your project(s).

robsoft
  • 5,525
  • 4
  • 35
  • 47
3

Note that in many cases, the user's expecation will be that launching the second instance results in the first instance being restored and brought to the foreground. Don't expect users to understand the difference between restoring a minimized/hidden app and launching from a shortcut or start menu.

Chris Thornton
  • 15,620
  • 5
  • 37
  • 62
  • 5
    Welcome to Stack Overflow. What you've posted here does not *answer* the question, so it doesn't belong in the answer section. It belongs in the comments. Once you've gained 50 reputation points, you'll be allowed to post comments to other people's questions and answers instead of just your own. Beware that answers that don't actually answer the question are prone to be voted down. – Rob Kennedy Mar 12 '10 at 14:48
1

In my experience one cannot decide in general wether an application my be started twice or not. It may be for instance perfectly valid to start the same application if it is started in another folder or under another user account or whatever. On the other hand it might be the case that two different applications may not run together if they are started in the same folder or so.

So besides the different approaches with mutexes and semaphores and handling race conditions, it is the wise selection of the mutex's or semaphore's name that handles the above combinations appropriately.

If an application may not run twice at all, take a GUID like name. You can even use the exe's filename if you can ignore that someone might rename it.

Restricting the one-time-start on a specific folder, you can take the exe path into account, but be aware that due to mappings different pathes may end up at the same exe.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130