Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Let the app only run once

Posted on 1998-12-19
8
212 Views
Last Modified: 2010-04-04
how can I check, whether my app is already run or not?
I mean: the user shall not be allowed to run my app more than one time. I have a dizzy idea of how this works, but I'm being lazy, so go ahead, get the points...  :-)

Greetings, Oli
0
Comment
Question by:Oli2
  • 5
  • 3
8 Comments
 

Accepted Solution

by:
Metty earned 100 total points
ID: 1352012
//the usual stuff above
var hwnd: Word;
 
begin
  if hPrevInst = 0 then
  begin
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end
  else
  begin
    hwnd := FindWindow('TForm1', nil);
    if (not IsWindowVisible(hwnd)) then
    begin
      ShowWindow(hwnd, sw_ShowNormal);
      PostMessage(hwnd, wm_User, 0, 0);
    end;
    else
      SetWindowPos(hwnd, HWND_TOP, 0,0,0,0,
        SWP_NOSIZE or SWP_NOMOVE);
  end;
end.
//====================================================

In the form's PAS file add a message response function for the wm_User
message.

//====================================================
{in the form declaration}
public
  procedure WMUser(var msg: TMessage); message wm_User;

{in the implementation section}
procedure TForm1.WMUser(var msg: TMessage);
begin
  Application.Restore;
end;

0
 
LVL 1

Author Comment

by:Oli2
ID: 1352013
Hi, Metty!
Thanx for the answer. I'm checking it. If it works out, you'll get the points. Give me an hour or two.

Greetings, Oli
0
 
LVL 1

Author Comment

by:Oli2
ID: 1352014
Hi, Metty!

Sorry, to say, but it don't work yet.

First of all, I get the error while compiling, in this line:
-->  PostMessage(hwnd, wm_User, 0, 0);  <--
Error: wm_User is undefined.

Maybe I should have told you before, that the app is an ShellNotifyIcon - application.

Here's some code:

begin
  Application.Initialize;
  Application.ShowMainForm := False;
  Application.CreateForm(TfrmMain, frmMain);
  ShowWindow ( Application.Handle, SW_HIDE );
  Application.Run;
end.

and from the .pas file:

public
  IconData : TNotifyIconData;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
     ShowWindow ( Application.Handle, SW_HIDE );
     IconData.cbSize := SizeOf ( IconData );
     IconData.szTip := 'Click here';
     IconData.Wnd := Handle;
     IconData.uID := 234;
     IconData.hIcon := Application.Icon.Handle;
     IconData.uCallbackMessage := WM_USER + 1;
     IconData.uFlags := NIF_ICON + NIF_MESSAGE + NIF_TIP;
     Shell_NotifyIcon( NIM_ADD, @IconData );
end;

procedure TfrmMain.WndProc ( var Msg: TMessage );
var       pos: TPoint;
begin
     case Msg.Msg of
     WM_User + 1 :
        case Msg.LParam of
             WM_RBUTTONDOWN:
                begin
                     SetForegroundWindow(Application.Handle);
                     Application.ProcessMessages;
                     GetCursorPos ( pos );
                     mnuMain.Popup ( pos.x, pos.y );
                end;
             WM_LBUTTONDOWN:
                begin
                    //Still to do !
                end;
        end;
     end;
     inherited;
end;

The Form is not shown at the start. Later on, it might become visible, if the user clicks on the popupmenu - "Show Form", but if he doesn't, the Form will alway be hidden.

Greetings, Oli

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Expert Comment

by:Metty
ID: 1352015
Sorry! My first code was totally crap (oops)!!! I've found this
somewhere and I planned to use this code too, but I've been totally busy
with the rest of my project. Well today I've implemented the code into my
file and realized that it's totally buggy! Well below is the corrected
and tested version :-)

//Place this in your *.dpr file:
Uses
  WinProcs,
  WinTypes;

Const AppName : 'My Application'; //Place your name here
var PrevInstance : hWnd;
     
begin
  PrevInstance := FindWindow('TApplication', AppName);
  if PrevInstance <> 0 then
    if IsIconic(PrevInstance) then
       ShowWindow(PrevInstance, sw_Restore)
    else
       BringWindowToTop(PrevInstance)
  else
  begin
    Application.Title := AppName;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end;
end.

I think this should work with your ShellnotifyIcon-App too
0
 
LVL 1

Author Comment

by:Oli2
ID: 1352016
Hi, Metty !
Well, at least this code doesn't bring up any compiling errors. Still the problem is: I can start my app as many times as I want :-(
Is it possible that my overriding of the WndProc is the problem ?
I'll try it.

Greetings, Oli
0
 

Expert Comment

by:Metty
ID: 1352017
Sorry again, but in my project all works fine now.
Perhaps you forgot the line:
"Application.Title := AppName;"

My app doesn't even use "Application.Initialize"!

By the way, what app do you want to program?

Ciao, Metty
0
 
LVL 1

Author Comment

by:Oli2
ID: 1352018
Sorry, it was my fault !!!
It works just fine now !
Since you had quiet some work, I'll adjust the points from 50 to 100, is that okay for you?

Thanks a lot !

Greetings, Oli
0
 
LVL 1

Author Comment

by:Oli2
ID: 1352019
You were right, I forgot Application.Title ... stupid me !

This app is kind of a trigger - I start other parts of the app from it. ( Some are parts of the same dpr, some are own dpr's ) This is why it's pretty important to have it run only once.

Greetings, Oli

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 221
Working this component WebCopy with proxy, help 3 92
Delphi Form ownership 4 110
Dev Express grid collapse 2 45
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question