SingleInstance OF an Application

Posted: April 24, 2005 in Uncategorized

Single Instance of an application is not Exactly the Singleton Model .Because in singleton model we talk about a class having only a single insatnce at most per application.However in singleinstance the requirement is often a single application instance(many time an exe) per system.(may be network :often central flag based system used the same..that is put a db entry of on flag and check the same before start.The most sutable is though keep the same running in a single system).

Now coming to the requirement :In singleton often we need a singleinstance to be shared thru out the apps thsu we have to design a thread safe static/shared(in vb) class with synchronization.However for single instance we need to manipulate the process class and us emutex to run the apps instance and if the instance is runnig  let return the instance .

here are some example and links:-

Many ways of single instance apps

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

http://www.codeproject.com/csharp/singleinstance.asp

http://www.ai.uga.edu/mc/SingleInstance.html

http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=711

In single system in vb.net

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

C#

[STAThread]static void Main() {     Application.Run(new Form1());}   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();    }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s