C# FAQ: How ensure that only one instance of an application will run
How ensure that only one instance of an application will run? |
| Often, it is necessary to be sure that only a single instance of an application will be running at a time. Examples include applications which control resources. There is a design pattern designed specifically for this purpose called the Singleton design pattern.
Another way of accomplishing this is by using a named mutex. The following example illustrates the creation of a mutex: bool instantiated; Mutex mutex = new Mutex (false, "Local\\" + aUniqueID, out instantiated); // If instantiated is true, this is the first instance // of the application; else, another instance is running. The mutex must be local—meaning it exists in the current user session. If the mutex were not local, users could share the mutex. Thus, two different users could not be running the program at the same time. Unlike various examples to be found on the Web, this sample code does not contain a call to a The mutex will not be garbage collected. Therefore, if a local variable is only used near the beginning of a method, the Garbage Collector (GC) may ignore it when determining which variables are garbage collection "roots" if that part of the method has already been executed. This can result in the mutex being released earlier than expected. To prevent premature release, make a call to: GC.KeepAlive (mutex); at the end of the Or, use a static variable to hold the mutex. That ensures that the mutex will not be garbage collected until the Another strategy is to "listen" on a local port. Since only one process can listen to a particular port, this fact can be exploited to ensure that only one instance of your application is running. But, there will be problems if another application tries to use that port for any purpose. Also, this strategy provides a communication channel between the initial instance and the new instance. For example, the new instance might want to tell the initial instance to open a user-requested file. This could be accomplished using the socket. |
More Visual C# and .NET FAQs
Go to Visual CSharp FAQ (main page).
Consider these related Frequently Asked Questions—FAQs:
- Am I missing an assembly reference?
- Are .NET generics like C++ templates?
- Are there free .NET decompilers?
- Does .NET run only on Windows?
- How call a method using a name string?
- How convert a
stringtype to aninttype? - How create an instance of a type using only its name?
- How display
intas a binary number, a string of 0s and 1s? - How enforce coding guidelines for custom .NET assemblies?
- How ensure that only one instance of an application will run?
- How get assembly attributes at runtime?
- How good are .NET decompilers?
- How protect .NET code against decompilation?
- How protect .NET code from reverse engineering?
- How start another program from .NET?
- How to read files with accented characters?
- Is there a .NET equivalent to
regsvr32? - Must the framework be installed to run .NET applications?
- Should calling
Initialize()on a reference type array fill it with objects? - What about .NET decompilers that allow code to be submitted from browsers?
- What compression and zipping capabilities does .NET have?
- What do all the .NET acronyms mean?
- What does a .NET decompiler do?
- What is .NET?
- What is the difference between
Convert.ToInt32(string)andInt32.Parse(string)? - What is the easiest way to fetch a Web page in .NET?
- Why does .NET make arithmetic errors?