Solved

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

Posted on 1997-07-21
11
353 Views
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.
0
Comment
Question by:wombat032397
  • 6
  • 4
11 Comments
 
LVL 6

Accepted Solution

by:
anthonyc earned 100 total points
Comment Utility
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
end if

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

Author Comment

by:wombat032397
Comment Utility
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.
0
 
LVL 6

Expert Comment

by:anthonyc
Comment Utility
Wombat,

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! :)

Anthony
 
0
 

Author Comment

by:wombat032397
Comment Utility
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"
  else
     text1 = "Starting program first time"
  end if
end sub

sub form_linkexecute(.....)
   text1 command$
end sub
0
 
LVL 6

Expert Comment

by:anthonyc
Comment Utility
wOM,

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
Anthony
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:wombat032397
Comment Utility
Ok, have change all the form1.xxx to label1.xxx 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
0
 
LVL 6

Expert Comment

by:anthonyc
Comment Utility
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.

Anthony
0
 

Author Comment

by:wombat032397
Comment Utility
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"
   else
      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?
0
 
LVL 5

Expert Comment

by:y96andha
Comment Utility
Have you set the linkmode of the form to 1 in the design mode? If not, then that's why it won't work.
0
 

Author Comment

by:wombat032397
Comment Utility
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?
0
 

Author Comment

by:wombat032397
Comment Utility
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.LinkPoke
    Text1.LinkMode = 0 ' close off the link
    Unload Me  'kill off the new instance
end if

and then in Sub Text2_Change()
   Text2.Visible

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.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now