• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

Simple Easy, Check For Instance Of Running Program

Hi Folks, sometime ago i had a bit of code that created an "atom" and when a program ran, it looked to see if it was already in memory if it was my delphi program would not run until it closed it down..... it was only a few lines long.....

I don't suppose anyone here has the code, or an alternative solution to this? :)

Thanks....

Craig C.
0
craig_capel
Asked:
craig_capel
  • 4
  • 3
  • 3
  • +1
1 Solution
 
LischkeCommented:
Hi Craig,

it is AddAtom, FindAtom and DeleteAtom in the Win32 API.

Ciao, Mike
0
 
edeyCommented:
Though Atoms are used for this purpose, it isn't generally recomended as the size of the atom table (and the atoms) is limited, one of those "provided for compatibility" things.  This is a pretty commonly asked question, though, so heres a previously asked question (should *always* try the search engine first :) on semaphores (found searching for "one instance"):

http://www1.experts-exchange.com/bin/ShowQ?qid=10121069&search=

GL
Mike
0
 
CalvinDayCommented:
I use something like this:

program xPrint;

uses
  Hide1,
  Hide2,
  Windows,
  Forms,
  uPrint in 'uPrint.pas' {fPrint},
  uAbout in 'uAbout.pas' {fAbout};

{$R *.RES}

begin
  CreateMutex(nil,false,'xPrint');
  if GetLastError=ERROR_ALREADY_EXISTS then
    begin
    SendMessage(HWND_BROADCAST,RegisterWindowMessage('xPrint'),0,0);
    Halt(0);
    end;

  Application.Initialize;
  Application.Title := 'xPrint';
  Application.CreateForm(TfPrint, fPrint);
  Application.Run;
end.


You can use the sendmessage to alert the other instance to wake up.

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
craig_capelAuthor Commented:
edey, although true, how would you get easy points?.....

ok just testing out a couple of your suggestions... Thanks all, brb....
0
 
craig_capelAuthor Commented:
sorry for the delay.... recovering from a hangover.... :)

But your code worked great..... Thanks a lot, sorry for the lack of points....
0
 
LischkeCommented:
Hi Craig,

please formulate your future questions a bit more carefully. I honestly thought you need to work with atoms not just to prevent your application from running twice. Otherwise I had given you an elegant solution. The broadcast thingy is really bad as it needs on some machine several seconds to return. For a precise question you can get really professional answers. Consider this...

Ciao, Mike
0
 
CalvinDayCommented:
Mike,

I agree with your statement about the broadcast. Setting up and trapping the broadcast is really a pain.

If you have a more elegant (hopefully simple) soluton to this please let us know. I will post a 50 point question for it.

Please post it here.
0
 
CalvinDayCommented:
Mike,

I agree with your statement about the broadcast. Setting up and trapping the broadcast is really a pain.

If you have a more elegant (hopefully simple) soluton to this please let us know. I will post a 50 point question for it.

Please post it here.
0
 
CalvinDayCommented:
Don't ya hate that.
0
 
craig_capelAuthor Commented:
I don't suppose anyone here has the code, or an alternative solution to this? :)

Lischke... Sorry, but i don't want to sound rude, but i did put in my very first comment.... "I don't suppose anyone here has the code, or an alternative solution to this? :)"

An alternative such as Calvin gave worked great for me.... yeah i just knew about the attoms.... i did not care how i did it, i was thinking about writing a file to disk or something, but thats BAD programming, then i thought about DDE, in the end i would just ask about code i had seen before... sorry :(

Thanks for your help again guys....

Craig C.
0
 
LischkeCommented:
Craig, perhaps it is the different languages we speak, so I did not understand the question as you meant it to sound. Perhpas next time...

Calvin, I'll have a look. (btw: Don't press the refresh button of your browser if you don't want to resend your formular data you entered when commenting. For this particular task there's a link on every question page, labeled "Reload question").

Ciao, Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now