Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 1997-07-21
11
Medium Priority
?
397 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
[X]
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
11 Comments
 
LVL 6

Accepted Solution

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

Expert Comment

by:anthonyc
ID: 1428620
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
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

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

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

Expert Comment

by:anthonyc
ID: 1428622
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
 

Author Comment

by:wombat032397
ID: 1428623
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
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.

Anthony
0
 

Author Comment

by:wombat032397
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"
   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
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.
0
 

Author Comment

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

Author Comment

by:wombat032397
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.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

Technology Partners: 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!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses

715 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