Single Instance of an Application
In windows applications there is a condition we often face where only one instance of our application is required. A best example could be the use of an external device connected to your COM port and your application trying to communicate to it via COM Port. As usual I will try my best to keep this article very informative For Visual Basic 6 developers, a bad news coz' there is no property like App.PrevInstance available from the App Global object (now Application class) directly. So go ahead and checkout the following code.
Steps:- Note:- Skip Step 1 to 3 if you already have an application
1> Goto File->New->Blank Solution 2> Select Visual C# Projects in Project Types 3> Select WindowsApplication in Templates 4> Add the following line on the top along with other namespaces.
using System.Diagnostics; 5> Search for [STAThread] in your application. 6> This is to specify Single Threaded Appartment thread
This means in the process there is only one thread which will accomplish all the task for the process. Tip:- To view the processes currently running you can go to Task Manager by pressing Ctrl + Shift + Esc (Only works in Win2000 & later)
Note:- Its mandatory to give [STAThread] for the main method in ver 1.0. Dot Net Framework 1.0 has this bug which has been removed in ver 1.1 where its non mandatory
[STAThread] static void Main() { Application.Run(new Form1()); }
7> Now Paste the following snippet right after the Main functions starts before the existing line.
Process aProcess = Process.GetCurrentProcess(); string aProcName = aProcess.ProcessName; if (Process.GetProcessesByName(aProcName).Length > 1) { MessageBox.Show("The application is already running!!","Test",MessageBoxButtons.OK,MessageBoxIcon.Stop); Application.ExitThread(); }
Thats it compile it and run the executable from the windows Explorer twice to test that it disallows the second copy.
The author is a Bachelor of Computer Application with Microsoft Certified Solution Developer. Currently working in a Mumbai's leading software developing firm Sysconnect Technologies India Pvt Ltd He has worked extensively with COM and has been programming since 1998. He has also trained more than 200 students and has taught various languages,RDBMS & platforms from C,C++ to VB with Oracle,Access & MS SQLServer from Vbscript,Javascript to ASP. His expertise comes from various industries viz Jewellery, Shipping ,Automobiles, Radio Frequency Devices, Photometers to name few. Besides programming he loves music, singing, dancing, bike riding & cricket.
|
| Author: Ashwini 17 Jul 2004 | Member Level: Bronze Points : 0 |
thanks
|
| Author: raj shekhar 19 Jul 2004 | Member Level: Bronze Points : 0 |
This article is very helpfull for removing in creation of new instance in .net
thanks and keep doing
|
| Author: Anbarasan 19 Jul 2004 | Member Level: Bronze Points : 0 |
This snippet really helped our project. I
|
| Author: Anbarasan 19 Jul 2004 | Member Level: Bronze Points : 0 |
This snippet really helped our project.
|
| Author: sd 21 Jul 2004 | Member Level: Bronze Points : 0 |
hi irfan u r right that it works.There are also several other tricks. like the one i am providing here
Imports System.Threading
Module modMain Public mtx As Mutex
Public Sub Main()
Dim bCreated As Boolean mtx = New Mutex(False, "SingleInstanceApp", bCreated) If bCreated = True Then
Application.Run(New frmMain) Else End End If
End Sub End Module
But the concern is when you invoke it from another system boith technique won't work. what i mean to say is if i create a shortcut from pc1 to pc2 and if the application is running in pc1 from pc2 it should not run but what i found in pc2 it simply runs.
for any comment and suggestions mail me at sndshreeman@rediffmail.com
|
| Author: Irfan Patel 21 Jul 2004 | Member Level: Silver Points : 0 |
That was so nice of you Mr Shreeman Narayan. We are here to help people facing the problems we have faced and above all by helping others we help ourself. Sometimes there remain holes. Thanx for filling them. True ! If we try to do what you said it would skip the Process check. Good point raise !! A well thought one !! Thanx
|
| Author: Andy 29 Jul 2004 | Member Level: Bronze Points : 0 |
If you are going to use a Mutex (which is the best way) then it is quite important that the name (ie. "SingleInstanceApp") is something quite unique to your application. Imagine your program not starting because someone else has used the same Mutex name as you!
I have also seen this example used with the 'aProcess.ProcessName', which is fine untill you decide you want to execute your app over the internet at which point the process name will always be "IEExec.exe" :-)
|
| Author: vinita 08 Sep 2004 | Member Level: Bronze Points : 0 |
i am not able to see any difference while executing.
|
| Author: Senthil kumar 20 Sep 2004 | Member Level: Bronze Points : 0 |
There is a design pattern to care that only one instance of a object is created.
Its called the singleton.
Please read on the following simple article to understand it.
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=486
|
| Author: sd 04 Oct 2004 | Member Level: Bronze Points : 0 |
hi senthil
i alraedy have gone thru the singleton pattern but i didnot found how to apply that to an process or xecutable if you have any example of how to implement a single copy of application running at any single time thru out the network then i ll appreciate most.
hi andy
i already followed up the mutex scenario but how could you pass the mutex cross domain to control the instance and how can you implement this:running a singleinstance of application at any giving time in the network.
thanks sndshreeman@rediffmail.com
further inviting you guysto join http://groups.msn.com/-netindiagroup for helping fellow devs.
shreeman
|
| Author: sd 04 Oct 2004 | Member Level: Bronze Points : 0 |
hi senthil
i alraedy have gone thru the singleton pattern but i didnot found how to apply that to an process or xecutable if you have any example of how to implement a single copy of application running at any single time thru out the network then i ll appreciate most.
hi andy
i already followed up the mutex scenario but how could you pass the mutex cross domain to control the instance and how can you implement this:running a singleinstance of application at any giving time in the network.
thanks sndshreeman@rediffmail.com
further inviting you guysto join http://groups.msn.com/-netindiagroup for helping fellow devs.
shreeman
|
| Author: sd 04 Oct 2004 | Member Level: Bronze Points : 0 |
hi senthil
i alraedy have gone thru the singleton pattern but i didnot found how to apply that to an process or xecutable if you have any example of how to implement a single copy of application running at any single time thru out the network then i ll appreciate most.
hi andy
i already followed up the mutex scenario but how could you pass the mutex cross domain to control the instance and how can you implement this:running a singleinstance of application at any giving time in the network.
thanks sndshreeman@rediffmail.com
further inviting you guysto join http://groups.msn.com/-netindiagroup for helping fellow devs.
shreeman
|
| Author: sd 04 Oct 2004 | Member Level: Bronze Points : 0 |
hi senthil
i alraedy have gone thru the singleton pattern but i didnot found how to apply that to an process or xecutable if you have any example of how to implement a single copy of application running at any single time thru out the network then i ll appreciate most.
hi andy
i already followed up the mutex scenario but how could you pass the mutex cross domain to control the instance and how can you implement this:running a singleinstance of application at any giving time in the network.
thanks sndshreeman@rediffmail.com
further inviting you guysto join http://groups.msn.com/-netindiagroup for helping fellow devs.
shreeman
|
| Author: sd 04 Oct 2004 | Member Level: Bronze Points : 0 |
hi senthil
i alraedy have gone thru the singleton pattern but i didnot found how to apply that to an process or xecutable if you have any example of how to implement a single copy of application running at any single time thru out the network then i ll appreciate most.
hi andy
i already followed up the mutex scenario but how could you pass the mutex cross domain to control the instance and how can you implement this:running a singleinstance of application at any giving time in the network.
thanks sndshreeman@rediffmail.com
further inviting you guysto join http://groups.msn.com/-netindiagroup for helping fellow devs.
shreeman
|
| Author: ALOK KUMAR 27 Jan 2008 | Member Level: Bronze Points : 0 |
I am very thank full to you for submitting such answer it is very very helpfull for me in making my project for doeacc b level thanks
|