PC Review


Reply
Thread Tools Rate Thread

Application instance

 
 
T Cordon
Guest
Posts: n/a
 
      8th Jul 2004
How to determine if another instance of the application is running so that
ir doesn't run multiple instances?


Thanks


 
Reply With Quote
 
 
 
 
Greg Burns
Guest
Posts: n/a
 
      8th Jul 2004
Option Strict On
Imports System.Threading

Module Main
Private Sub SubMain()

SingletonApp.Run(New MyForm)

End Sub
Enc Module


Public Class SingletonApp

Shared m_Mutex As Mutex
Public Shared Sub Run(ByVal mainForm As Form)
If (IsFirstInstance()) Then
AddHandler Application.ApplicationExit, AddressOf OnExit
Application.Run(mainForm)
End If
End Sub
Public Shared Function IsFirstInstance() As Boolean

' use this to create a unique guid for your app, or Tools->Create
GUID...
'Dim g As New Guid
'g = Guid.NewGuid
'Debug.WriteLine(g.ToString)

m_Mutex = New Mutex(False, "8ca35a66-6e9a-41d4-a87d-d9755b1f88c4") '
arbitrary GUID
Dim owned As Boolean = False
owned = m_Mutex.WaitOne(TimeSpan.Zero, False)
Return owned
End Function

Public Shared Sub OnExit(ByVal sender As Object, ByVal args As
EventArgs)
m_Mutex.ReleaseMutex()
m_Mutex.Close()
End Sub
End Class

"T Cordon" <(E-Mail Removed)> wrote in message
news:O$(E-Mail Removed)...
> How to determine if another instance of the application is running so that
> ir doesn't run multiple instances?
>
>
> Thanks
>
>



 
Reply With Quote
 
Klaus H. Probst
Guest
Posts: n/a
 
      8th Jul 2004
The only sure way is to create a Mutex and try to acquire ownership of it
every time your app opens.

Luckily using a mutex from .NET is simple.

--
Klaus H. Probst, MVP
http://www.vbbox.com/


"T Cordon" <(E-Mail Removed)> wrote in message
news:O$(E-Mail Removed)...
> How to determine if another instance of the application is running so that
> ir doesn't run multiple instances?
>
>
> Thanks
>
>



 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      8th Jul 2004
Hi,

Start your app from sub main use a mutex to see if there is another
instance running.

Public Sub main()

Dim owned As Boolean

Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3", owned)

If owned Then

Application.Run(New Form1)

mut.ReleaseMutex()

Else

MessageBox.Show("A previous instance is already running")

End If

End Sub



Ken

------------------------

"T Cordon" <(E-Mail Removed)> wrote in message
news:O$(E-Mail Removed)...
> How to determine if another instance of the application is running so that
> ir doesn't run multiple instances?
>
>
> Thanks
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      8th Jul 2004
* "T Cordon" <(E-Mail Removed)> scripsit:
> How to determine if another instance of the application is running so that
> ir doesn't run multiple instances?


<URL:http://www.pobox.com/~skeet/csharp/faq/#one.application.instance>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
 
Reply With Quote
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      8th Jul 2004
Windows forms Tips and Tricks has an example that also brings the running
instance to the foreground.

http://www.bobpowell.net/tipstricks.htm

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml






"T Cordon" <(E-Mail Removed)> wrote in message
news:O$(E-Mail Removed)...
> How to determine if another instance of the application is running so that
> ir doesn't run multiple instances?
>
>
> Thanks
>
>



 
Reply With Quote
 
=?Utf-8?B?SiBIdWdoZXM=?=
Guest
Posts: n/a
 
      8th Jul 2004
Having detected another instance of the application is it possible to make that instance become the active application instead of
MessageBox.Show("A previous instance is already running")

Thanks


"Ken Tucker [MVP]" wrote:

> Hi,
>
> Start your app from sub main use a mutex to see if there is another
> instance running.
>
> Public Sub main()
>
> Dim owned As Boolean
>
> Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3", owned)
>
> If owned Then
>
> Application.Run(New Form1)
>
> mut.ReleaseMutex()
>
> Else
>
> MessageBox.Show("A previous instance is already running")
>
> End If
>
> End Sub
>
>
>
> Ken
>
> ------------------------
>
> "T Cordon" <(E-Mail Removed)> wrote in message
> news:O$(E-Mail Removed)...
> > How to determine if another instance of the application is running so that
> > ir doesn't run multiple instances?
> >
> >
> > Thanks
> >
> >

>
>
>

 
Reply With Quote
 
The Grim Reaper
Guest
Posts: n/a
 
      9th Jul 2004
Um.... I might be missing something blindling obvious here... but when I
upgraded some VB6 code the other day, it changed my code to this;

Public Sub Main()
Try
' Disallow other copies running
If GetProcessesByName(GetCurrentProcess.ProcessName).GetUpperBound(0)
= 0 Then
Application.EnableVisualStyles()
Application.Run(New frmMain)
End If
Catch vException As Exception
MessageBox.Show(vException.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

I take it this code neglects to let your application run if another process
with the same name also happens to be running... I suppose if your app is
called MyDatabaseAppOnMyPC.exe, the chances of a conflict are low.. lol
_______________________
The Grim Reaper

"T Cordon" <(E-Mail Removed)> wrote in message
news:O$(E-Mail Removed)...
> How to determine if another instance of the application is running so that
> ir doesn't run multiple instances?
>
>
> Thanks
>
>



 
Reply With Quote
 
Greg Burns
Guest
Posts: n/a
 
      9th Jul 2004
Curious. What did the VB 6 code look like before the upgrade?

Yes, simply checking for the same named process is not very full proof. I
am surprised the upgrade wizard would go that route. A mutex seems to be the
safer bet.

Greg

"The Grim Reaper" <(E-Mail Removed)> wrote in message
news:cckklf$cm2$(E-Mail Removed)...
> Um.... I might be missing something blindling obvious here... but when I
> upgraded some VB6 code the other day, it changed my code to this;
>
> Public Sub Main()
> Try
> ' Disallow other copies running
> If GetProcessesByName(GetCurrentProcess.ProcessName).GetUpperBound(0)
> = 0 Then
> Application.EnableVisualStyles()
> Application.Run(New frmMain)
> End If
> Catch vException As Exception
> MessageBox.Show(vException.Message, "Error", MessageBoxButtons.OK,
> MessageBoxIcon.Error)
> End Try
> End Sub
>
> I take it this code neglects to let your application run if another
> process
> with the same name also happens to be running... I suppose if your app is
> called MyDatabaseAppOnMyPC.exe, the chances of a conflict are low.. lol
> _______________________
> The Grim Reaper
>
> "T Cordon" <(E-Mail Removed)> wrote in message
> news:O$(E-Mail Removed)...
>> How to determine if another instance of the application is running so
>> that
>> ir doesn't run multiple instances?
>>
>>
>> Thanks
>>
>>

>
>



 
Reply With Quote
 
The Grim Reaper
Guest
Posts: n/a
 
      9th Jul 2004
Oh yes, I agree the mutex thing looks a whole lot more solid!! The old VB6
code (I've found it in most of my old projects after a brief search - dunno
where I got it from) was something like this;

Sub Form_Load()
If App.PrevInstance Then
Msgbox "<app name> is already running!", vbOKOnly + vbInformation,
"Stupid User Alert"
Unload Me
End If
End Sub

Seems oh-so-simple looking back... lol.
____________________________
The Grim Reaper

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:(E-Mail Removed)...
> Curious. What did the VB 6 code look like before the upgrade?
>
> Yes, simply checking for the same named process is not very full proof. I
> am surprised the upgrade wizard would go that route. A mutex seems to be

the
> safer bet.
>
> Greg
>
> "The Grim Reaper" <(E-Mail Removed)> wrote in message
> news:cckklf$cm2$(E-Mail Removed)...
> > Um.... I might be missing something blindling obvious here... but when I
> > upgraded some VB6 code the other day, it changed my code to this;
> >
> > Public Sub Main()
> > Try
> > ' Disallow other copies running
> > If

GetProcessesByName(GetCurrentProcess.ProcessName).GetUpperBound(0)
> > = 0 Then
> > Application.EnableVisualStyles()
> > Application.Run(New frmMain)
> > End If
> > Catch vException As Exception
> > MessageBox.Show(vException.Message, "Error", MessageBoxButtons.OK,
> > MessageBoxIcon.Error)
> > End Try
> > End Sub
> >
> > I take it this code neglects to let your application run if another
> > process
> > with the same name also happens to be running... I suppose if your app

is
> > called MyDatabaseAppOnMyPC.exe, the chances of a conflict are low.. lol
> > _______________________
> > The Grim Reaper
> >
> > "T Cordon" <(E-Mail Removed)> wrote in message
> > news:O$(E-Mail Removed)...
> >> How to determine if another instance of the application is running so
> >> that
> >> ir doesn't run multiple instances?
> >>
> >>
> >> Thanks
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
One Instance of two application LirenZhao Microsoft Dot NET Compact Framework 5 8th Jul 2005 07:47 AM
Application Instance T Cordon Microsoft Dot NET Framework Forms 1 8th Jul 2004 05:50 PM
Multi-instance vs single instance application kathy Microsoft C# .NET 3 7th Jan 2004 11:53 PM
How can mimed Application files open in existing application instance? TomShelley Microsoft Dot NET Framework Forms 1 13th Nov 2003 02:13 AM
Re: Run only 1 instance of the application Dennis Peterson Microsoft Dot NET Framework Forms 0 12th Aug 2003 07:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:36 PM.