App already running?

  • Thread starter Thread starter Bradley
  • Start date Start date
B

Bradley

Hey all,
Another convert to vb.net here. My vb6 program was an application
launcher that checked for previous instances of said programs and would
alert the user with a message box. I can't, for the life of me, figure
out how to do this in vb.net. Let's say notepad.exe (as an example) is
already running. My program tries to launch it again, but I need to
check if notepad.exe is already running before I launch it.

Does anyone have a snipped to code to help me out here? I've googled
my arse off and the closest I can get is to check for a previous
instance of /my/ app, and I can't even get /that/ to work.

Sounds simple, but I'm stumped.

Thanks for any help!

-b
 
I'm sure there's a windows API that will do just what you want. I may be able
to find it, but don't have time right now. Meanwhile, good luck
 
Here's some code I created for VB6 --

Public Declare Function GetWindow Lib "user32" ( _
ByVal hwnd As Long, _
ByVal wCmd As Long _
) As Long

Public Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" ( _
ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long _
) As Long

Public Declare Function GetWindowTextLength Lib "user32" _
Alias "GetWindowTextLengthA" ( _
ByVal hwnd As Long _
) As Long

Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_OWNER = 4
Public Const GW_CHILD = 5


Sub LoadTaskList(FHandle As Long, Wins$())
'loads captions and handles of all windows in Wins$ array
Dim hwnd As Long, L, cap$, n
'Dim grd As MSFlexGrid
ReDim Wins$(2, 0)

n = 0
hwnd = GetWindow(FHandle, GW_HWNDFIRST)
Do While hwnd <> 0
L = GetWindowTextLength(hwnd)

cap$ = Space$(L + 1)
L = GetWindowText(hwnd, cap$, L + 1)

If L > 0 Then
n = n + 1
ReDim Preserve Wins$(2, n)

Wins$(1, n) = cap$
Wins$(2, n) = hwnd
End If

hwnd = GetWindow(hwnd, GW_HWNDNEXT)
DoEvents
Loop
End Sub

Function GetWinHandle(ByVal cap$, FHandle As Long) As Long
Dim n, i, t$, hwnd As Long, Found As Boolean, Wins$()
GetWinHandle = 0

LoadTaskList FHandle, Wins$()

cap$ = UCase(cap$)

n = UBound(Wins, 2)
For i = 1 To n
t$ = Wins$(1, i)
t$ = UCase(t$)
If InStr(t$, cap$) > 0 Then
Found = True
hwnd = Val(Wins$(2, i))
Exit For
End If
Next i

If Found Then GetWinHandle = hwnd
End Function

Function IsAppActive(ap$, whandle As Long) As Boolean
Dim h
h = GetWinHandle(ap$, whandle)
IsAppActive = (h > 0)
End Function


(I'm not an expert, so check this out thoroughly yourself!)
 
Hello Bradley,

Consider System.Diagnostics.Process.GetProcesses()

You could compare the executable names.

-Boo
 
Bradley said:
Another convert to vb.net here. My vb6 program was an application
launcher that checked for previous instances of said programs and would
alert the user with a message box. I can't, for the life of me, figure
out how to do this in vb.net. Let's say notepad.exe (as an example) is
already running. My program tries to launch it again, but I need to
check if notepad.exe is already running before I launch it.

Check out 'System.Diagnostics.Process.GetProcess*'.
 
The following function should return true if another instance is running.
You can check this in the Sub Main or whever using "Application.Exit" or
Form.Close methods depending on how your are starting your application.

Private Function PrevInstance() As Boolean
If
UBound(System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Function
 
Hello Bradley,

The Mutex option will not work. If someone opens the target application
using a means other than the launcher then the Mutex will not exist and the
app will now be open twice. Conversely, someone could open the app from
the launcher and then open it using another means. The mutex would exist
but the 'other means' doesnt give two hoots about the mutex.

-Boo
 
Dennis said:
The following function should return true if another instance is running.
You can check this in the Sub Main or whever using "Application.Exit" or
Form.Close methods depending on how your are starting your application.

Private Function PrevInstance() As Boolean
If
UBound(System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Function

Thanks all, great help! Question Dennis: How would the above code look
for a second instance of, say, notepad.exe? (I'll admit it, I'm greener
than green on vb.net...)

-b
 
What does "xvcjsdf67AS124#$3" refer to? Anything in particular?Something that probably does not exist already

:-)

Cor
 
Herfried,

Not as it is described, but I try forever not to read characters and words,
but what it can be. And than it is for me the best solution. I think that
the OP means with notepad a sample, not the exact application.

Cor
 
Cor Ligthert said:
Not as it is described, but I try forever not to read characters and
words, but what it can be. And than it is for me the best solution. I
think that the OP means with notepad a sample, not the exact application.

That's true, but the mutex sample will only work for your own application
whereas the OP wants to write a launcher application that prevents starting
applications more than once.
 
Hello Bradley,
The Mutex option will not work. If someone opens the target
application using a means other than the launcher then the Mutex will
not exist and the app will now be open twice. Conversely, someone
could open the app from the launcher and then open it using another
means. The mutex would exist but the 'other means' doesnt give two
hoots about the mutex.

Ah, herein lies the trick. Don't open the app from a launcher, but open it
directly. On the open, check the mutex. If it exists, the app is already
running. If not this is a first instance. If you are using 2005, SingleInstance
is extremely easy, otherwise it is a bit more tricky and interesting. I have
a write-up at http://devauthority.com/blogs/jwooley/archive/2005/07/28/318.aspx.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
Perhaps you are looking for something like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim prg As String
prg = InputBox("what program are we looking for?", "", "notepad")
MessageBox.Show(HowManyRunning(prg) & " instances of " & prg & " are
currently running")
End Sub

Private Function HowManyRunning(ByVal program As String) As Int32
Return
UBound(System.Diagnostics.Process.GetProcessesByName(program)) + 1
End Function
 
Herfried,
That's true, but the mutex sample will only work for your own application
whereas the OP wants to write a launcher application that prevents
starting applications more than once.
I know, but why would you want to stop the behaviour from programs from
others. Are you than not in fact changing behaviour of software from others,
having the change that somebody bails you because you can give a wrong idea
about that software or just claims the cost by instance after long calls
with the servicedesk from that software.

Just my thought,

Cor
 
Cor Ligthert said:
I know, but why would you want to stop the behaviour from programs from
others. Are you than not in fact changing behaviour of software from
others, having the change that somebody bails you because you can give a
wrong idea about that software or just claims the cost by instance after
long calls with the servicedesk from that software.

Huh? Who except you wants to change the behavior of software here? The OP
simply wants to start an application only if it is not already running.
This is a common feature in application launcher applications.
 
Herfried,
Huh? Who except you wants to change the behavior of software here? The
OP simply wants to start an application only if it is not already running.
This is a common feature in application launcher applications.
So you start an instance of Internet Explorer and you want to start a second
instance (assuming you have not IE 7 beta, what you probably use) or by
instance Excel or Word. And it will not start. What do you do, call
Microsoft, go to a newsgroup, Google?

Can you remember that guy/girl some years ago that did want to do some; what
he was calling funny things for his collegues?

If it is his own program, than that what I showed is in my idea the best.
Not mine however what I remember me as a discussions between You, Jon and
Tom and in my idea in a better way written on our website by Ken.

Cor
 
Back
Top