Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

only one instance of application? 2

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
I had a good read around, and this seemed to be perfect:


It stopped me running a second instance, and popped the first to front, if not already there. Great!

But then I discovered if I copied the same exe to another folder, and ran that one, I could get two instances.

Is there a (not too complicated!) way to capture more than one instance of the same program, including when run from different places?


Steve (Delphi 2007 & XP)
 
I have an application that uses this code in the .dpr:

Code:
var
  RvHandle : hWnd;

begin
    RvHandle := FindWindow(WIN_CLASS_NAME, NIL);
    if RvHandle > 0 then
    begin
      PostMessage(RvHandle, CM_RESTORE, 0, 0);
      Exit;
    end;

    Application.Initialize......

In my unit with the main form I have these constants:
Code:
...
const
    CM_RESTORE = WM_USER + $1000; //2002-10-20 (JDA)
    WIN_CLASS_NAME = 'Pervasive Peruser';

type
  TfrmMain = class(TForm)
    MainDatabase: TtbDatabase;
  ...

These are added as public procedures in the main form:
Code:
...
procedure CreateParams(var Params: TCreateParams); override;
procedure RestoreRequest(var message: TMessage); message CM_RESTORE;

Code for above:
Code:
procedure TfrmMain.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.WinClassName := WIN_CLASS_NAME;
end;

procedure TfrmMain.RestoreRequest(var message: TMessage);
begin
  if IsIconic(Application.Handle) = TRUE then
    Application.Restore
  else
    Application.BringToFront;
end;

 

Thanks djangman, another way to skin a cat..

I tried that on my pesky app, still doesn't pop up from minimised.

Some point down the line I'll try to go thru my app and see if I can make these offered solutions work completely.


Steve (Delphi 2007 & XP)
 
If I could chime in with one suggestion. Creating a semaphore is the way to go. But often programmers use the complete path of the app or a name they made up as the "unique" name for the
semaphore. That's where you'll run into problems such as an app will start from a different folder. What I do is use a GUID creation tool to create a GUID string and paste it into the app as the semaphore name. The only "fly in the ointment"
is even if you play with 'Global\' namespace to limit the one app across user switching, it doesn't always work.

As for restoring the minimized app.. that never seemed to work well either. I think in JVCL library what they do is have an application defined message that they broadcast and let the minimized app restore itself when it gets it. Just sending a standard windows message doesn't seem to do it.
 
CADTenchy, I'm glad I read through this thread again because I fixed up one of my apps to restore if minimized in the situation. :) In a lot of cases, what you want to do if somebody launches a 2nd instance of your app, is to transfer the command tail to the 1st instance, then let the 2nd instance die.. and optionally restore the 1st instance's window if minimized. From this I take the approach that it's up to the 1st instance to restore itself if minimized.

I rolled my own control that uses a named mutex with the guid as I mentioned but it also transfers the command tail and info about the 2nd instance. So in the 1st instance when you check if you were passed any data, you know if the user launched a 2nd instance. Then you just check if you are currently minimized and if so, show your window. Better than trying to use a Windows message to the WndProc of the 1st instance because each app is different. One may be a task tray icon app, mdi or dialog or whatever, so let the app straighten itself out once it knows another instance got launched.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top