Don't open more than 1 instance of a *different* program

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

Hello there,

I've seen several posts requesting help on not allowing a user to open
more than one instance of the programmer's program. What I haven's
seen yet is a post on not opening more than one instance of a
different program, and I'm having a hard time figuring it out
myself...

My project has a word processor portion, to which I've added an
Insert->Symbol menu. This simply calls the Character Map application
with the following code:

Try
Process.Start("charmap.exe")
Catch ex As Exception
MessageBox.Show("The Character Map...")
End Try

That seems to work well for me, but the user could open Character Maps
endlessly. If they have already opened one is there a way I can check
before I run the above code to open another one? (And for bonus
points... is there a way I could switch to the already opened
Character Map?)

(And if anyone has any suggestions on a better way to insert symbols
I'd appreciate any feedback.)

Thanks!
Kevin
 
Kevin,

I suggest you read the .Net documentation. Take a look at the
System.Diagnostics.Process class, it has methods like GetProcessesByName.

Jerry
 
Thanks for the simple advice --- sometimes I get so used to searching for my answers on newsgroups I forget about Microsoft's documentation. The documentation helped me figure out how to ensure that my project does not open up more than one copy of Character Map, but I'm left with two questions. 1) Is it normal for the first line of the new code below, GetProcessesByName, the first time it is called after running the project, to do a loud nasty grinding noise on the floppy drive, as if you're clicking on the A: without a disk in it? 2) I couldn't find how to bring the existing Character Map to the front. I saw a couple API calls that might work, but is there a DotNet way to do it

Thanks
Kevi

Dim p As Process() = Process.GetProcessesByName("charmap"

If p.Length = 0 The
Tr
Process.Start("charmap.exe"
Catch ex As Exceptio
MessageBox.Show(ex.ToString
End Tr
End If
 
1) I think so.

2) I don't know, I haven't done any client side coding in .net. It's really
trivial in Win32 API (even though you may not actually bring the window to
front, stealing focus from the current window).

Jerry

Kevin said:
Thanks for the simple advice --- sometimes I get so used to searching for
my answers on newsgroups I forget about Microsoft's documentation. The
documentation helped me figure out how to ensure that my project does not
open up more than one copy of Character Map, but I'm left with two
questions. 1) Is it normal for the first line of the new code below,
GetProcessesByName, the first time it is called after running the project,
to do a loud nasty grinding noise on the floppy drive, as if you're clicking
on the A: without a disk in it? 2) I couldn't find how to bring the
existing Character Map to the front. I saw a couple API calls that might
work, but is there a DotNet way to do it?
 
Yay! I think I found a simple .NET way to ensure that my program only opens one instance of Character Map, and it brings the focus to an already opened Character Map. The only thing it doesn't account for is if the Character Map is minimized AppActivate doesn't work... but I can tolerate that for now. Thanks for your help

Public Sub OpenCharacterMap(
Dim p As Process() = Process.GetProcessesByName("charmap"

If p.Length = 0 The
Tr
Process.Start("charmap.exe"
Catch ex As Exceptio
MessageBox.Show("The Character Map..."
End Tr
Els
AppActivate(p(0).Id
End I
End Sub
 
Back
Top