How to detect if my application(xx.exe) is already running?

Posted on 1997-07-21
Last Modified: 2008-03-06
I would like to know how I can detect if my application is
already running? The reason is that, I would like to avoid
my application being started again if the application is already running, why not just use the running app. And I would not have more than one of my application sitting on the taskbar. I have seen some program/application being able to handle this (eg, Microsoft Word.. that when I double click on xx.doc, it will start MSWord.exe, but if MSWord.exe
is already running, then it will just use that window, and open up this xx.doc file).

Any sample or document related to this will be great. Thanx.
Question by:wombat032397
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
  • 6
  • 4

Accepted Solution

anthonyc earned 100 total points
ID: 1428618
Here is something that will help (assuming you are using VB4 or vb5)

in your startup sub (form1_load or sub main)

if app.previnstance then
   msgbox "I am already running!"
end if

You can use FindWindow API to get the window handle of the other app, and set yourself to it

Author Comment

ID: 1428619
Ok, I have tried that, and it works, but if the application is
already running, it will end this application.
How can I "unload" the previous form, and load it up again with
the new information? Just like the example I gave using MSWord.exe. I still want the application to be started once, and when I click on different documents, it will "reload"/"refresh" the form.

Expert Comment

ID: 1428620

What I gave you will end the second instance, leaving the first instance still running.  What you need, which I didn't understand, was to have the next "document" load inside the MDI window (similar to Word, Excel).  This is going to be a little tougher.  You can use OLE Automation or DDE for this.  When you see that App.PrevInstance is true, instead of just msgbox to the user (simple example), you can kick off an OLE/DDE connection (your choice, although OLE is the way of the future), passing the document name to the first instance:

To do this, you need to write 2 subs.  One that will kick off the OLE call (in the if block with app.previnstance) and the second, in the first instance that will be able to handle the "open document" call.  I suspect you already have this one written.

I'll admit I am better at DDE, so this is what I will tell you to do.

On your MDI form in your application, set a linkMode = 1 (source).  Set the linktopic to a name, let's say SourceForm

Say your applicatioin (EXE Name) is SourceEXE.

do this:

if app.previnstance
    form1.linkmode = vbnone
    form1.linktopic = "SourceEXE|SourceForm"
    form1.linkmode = vbLinkManual

    form1.linkexecute command$ 'or whatever string you use to signify the document path
end if

in the Form's LinkExecute event, place code to read the string passed to get the form path.

Hope this helps..  and hope I didn't forget anything! :)

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1428621
Thanx, just one thing, I will get Method or Data Member not found
at Form1.LinkExecute Cmd$ during compilation.
Here's an example of my code: (can you pls let me know what I have done wrong here.)
private sub form_load()
  if app.previnstance then
     form1.linkmode = vbnone
     form1.linktopic = "Project1|Form1"
     form1.linkmode = vbLinkManual
     form1.linkexecute "second time"
     text1 = "Starting program first time"
  end if
end sub

sub form_linkexecute(.....)
   text1 command$
end sub

Expert Comment

ID: 1428622

I knew I'd forget something.....

You need to put a label, picturebox, or textbox on your form.  I usually use a label.  You can make it invisible, so it is not seen.  Where you have form1.linkmode, form1.linktopic, etc replace that with label1.linkmode, label1.linkexecute, etc

Then you should be all set...

Sorry about the mistake

Author Comment

ID: 1428623
Ok, have change all the to in the form_load
subroutine. However, I will get this error message, "No foreign
application responded to a DDE initiate" when I click on the
xxx.exe the second time. At the first click, every thing was ok,
my textbox1 will have "first call" printed onto it. So, when I
click xxx.exe the second time, I would "hope" to see that the
textbox1 would show "second time" instead of "fist call"

What happen? please help out here. Thanx

Expert Comment

ID: 1428624
Did you create a LinkExecute Procedure in your applications main form, (the one with the link topic) so it knows to respond?  It doesn't sound like you did.


Author Comment

ID: 1428625
What I did was something like this...(am not sure it is correct.
if it is not, then can you pls direct me how to do this... )

private sub Form_Load()
   if app.previnstance then
      label1.linkmode = vbnone
      label1.linktopic = "Project1|Form1"
      label1.linkmode = vbLinkManual
      label1.linkexecute "second time"
      text1 = "Starting program first time"
   end if
end sub

Private Sub Form_LinkExecute(CmdStr As String, Cancel As Integer)
    Cancel = False
    Text1 = "CmdStr = " & CmdStr
End Sub

am I missing something here?

Expert Comment

ID: 1428626
Have you set the linkmode of the form to 1 in the design mode? If not, then that's why it won't work.

Author Comment

ID: 1428627
Thanx. Have changed it and now I have another run-time error...when I run the application the second time.

"Foreign application won't perform DDE method or operation."

How can I solve this one?

Author Comment

ID: 1428628
anthony, I have managed to solve this problem and here is how I
did it.

in Form_Load
If App.PrevInstance Then
    Text1.Visible = False
    Text2.Visible = False
    Text1.Text = "hello world"
    Text1.LinkTimeout = -1
    Text1.LinkMode = 0
    Text1.LinkTopic = "Project1|Form1"
    Text1.LinkItem = "Text2"
    Text1.LinkMode = 2
    Text1.LinkMode = 0 ' close off the link
    Unload Me  'kill off the new instance
end if

and then in Sub Text2_Change()

so instead of using linkexecute, I have used linkpoke and I will have to set this Form1.LinkMode = Source, Form1.LinkTopic = Form1
inorder for it to accept this DDE statements.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

710 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