Solved

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

Posted on 1997-07-21
11
375 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
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
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!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Script Host failed (Access is denied.) error 6 314
vb6 connector to mongodb 2 138
Help me. 3 68
RUNRMTCMD from AS/400 12 83
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 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