Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

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

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
wombat032397
Asked:
wombat032397
  • 6
  • 4
1 Solution
 
anthonycCommented:
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
 
wombat032397Author Commented:
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
 
anthonycCommented:
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
Independent Software Vendors: 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!

 
wombat032397Author Commented:
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
 
anthonycCommented:
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
 
wombat032397Author Commented:
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
 
anthonycCommented:
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
 
wombat032397Author Commented:
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
 
y96andhaCommented:
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
 
wombat032397Author Commented:
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
 
wombat032397Author Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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