| | |
Check for Single-Instance
Please support our C# advertiser: Intel Parallel Studio Home
A simple way to check if the current C# program is the only instance of itself running.
private bool IsSingleInstance() { foreach (Process process in Process.GetProcesses()) { if (process.MainWindowTitle == this.Text) return false; } return true; }
0
•
•
•
•
this give error....
In foreach line (in process)
always mention namespace which is required and that place where code to be paste
In foreach line (in process)
always mention namespace which is required and that place where code to be paste
1
•
•
•
•
Simple, yes... but not something I'd allow into production code. See these links for some discussion.
Here's an alternative... first, a method to try to create a mutex unique to the application:
Here's how you'd use the method:
Here's an alternative... first, a method to try to create a mutex unique to the application:
C# Syntax (Toggle Plain Text)
private Mutex GetSingleInstanceMutex() { bool createdNewMutex; Mutex mutex = new Mutex(false, @"Global\" + SomethingUnique, out createdNewMutex); if (!createdNewMutex) { mutex = null; } return mutex; }
SomethingUnique should be something unique to the application, for example, the assembly GUID.Here's how you'd use the method:
C# Syntax (Toggle Plain Text)
using (Mutex mutex = GetSingleInstanceMutex()) { if (mutex != null) { // This is the only running instance; go ahead and run the application. } else { // Another instance is running; notify user and quit. } }
Similar Threads
- How do I declare a single Object instance then reuse it in each ComboBox? (VB.NET)
- converting Class instance to Web Service instance in C# (RSS, Web Services and SOAP)
- Making a Single Instance Application Reappear (C#)
- Single instance (Visual Basic 4 / 5 / 6)
| Thread Tools | Search this Thread |
.net access ado.net algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client combobox control conversion csharp custom database databasesearch datagrid datagridview datagridviewcheckbox dataset datetime degrees development draganddrop drawing dynamiccreation encryption enum equation event excel file form format formatting forms function gdi+ httpwebrequest image index input install interface java label list listbox mandelbrot math mouse mouseclick mysql namevaluepairs operator path photoshop picturebox pixelinversion post powerpacks programming property radians regex remote remoting resource restore richtextbox server sleep socket sql statistics stream string table text textbox thread time timer update usercontrol validation visualstudio wait webbrowser windows winforms working wpf xml



