Newbie question

  • Thread starter Thread starter Tony Vitonis
  • Start date Start date
T

Tony Vitonis

Hello. I'm a long-time programmer who's learning Windows and VB.NET, and as
part of that enterprise, I've written a little app that synchronizes the
system time to that of a NIST time server.

I'd like only a single instance of the program to run -- that is, if an
instance of the program is already running and the user requests another,
I'd like the second request to produce a message window and quit. Does
someone out there have an example of VB.NET code that does this? Thanks
very much.
 
* "Tony Vitonis said:
I'd like only a single instance of the program to run -- that is, if an
instance of the program is already running and the user requests another,
I'd like the second request to produce a message window and quit. Does
someone out there have an example of VB.NET code that does this? Thanks
very much.

<http://www.google.de/[email protected]>
 
Hello Tony,

here's a simple way:

Dim processes As Process() =
Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)

If (Not processes Is Nothing And processes.Length > 1) Then
MessageBox.Show("An instance of the application is already
running")
Return
End If

You can put the code in the Main method of your application.


Another way is to create some mutex and verify it is unique (taken from
another thread):

Dim bFirstInstance As Boolean
Dim m = New System.Threading.Mutex(True, "MyApplicationUniqueName",
bFirstInstance)

If bFirstInstance = False Then
MessageBox.Show("An instance of the application is already
running")
Return
End If


HTH

Antoine
Microsoft Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
NNTP-Posting-Date: Sun, 12 Oct 2003 13:30:29 -0500
From: "Tony Vitonis" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
Subject: Newbie question
Date: Sun, 12 Oct 2003 14:30:27 -0400
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Lines: 11
NNTP-Posting-Host: 68.55.85.183
X-Trace: sv3-cTLoA2CggwTkE2GSAuWZNRZL2VovROiIkY0AAn2KOAOcPGBeYJXR8mL74zGOuqCjmQKBLcKq
otlBg25!9ovhqXLzEoa/aGO8aSVYC2n7JXSqOF5plkLptMUV1V9sNY5SJZ+hIBjf1jC6
X-Complaints-To: (e-mail address removed)
X-DMCA-Complaints-To: (e-mail address removed)
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.1
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.co
m!news.maxwell.syr.edu!wn14feed!worldnet.att.net!216.166.71.14!border3.nntp.
aus1.giganews.com!intern1.nntp.aus1.giganews.com!nntp.giganews.com!nntp.comc
ast.com!news.comcast.com.POSTED!not-for-mail
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:146105
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hello. I'm a long-time programmer who's learning Windows and VB.NET, and as
part of that enterprise, I've written a little app that synchronizes the
system time to that of a NIST time server.

I'd like only a single instance of the program to run -- that is, if an
instance of the program is already running and the user requests another,
I'd like the second request to produce a message window and quit. Does
someone out there have an example of VB.NET code that does this? Thanks
very much.


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top