| | |
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 algorithm alignment app array barchart bitmap box broadcast c# c#gridviewcolumn cast check checkbox client combobox communication control conversion csharp custom database datagrid datagridview dataset datatable datetime degrees development draganddrop drawing elevated encryption enum excel file focus form format forms function gdi+ hospitalmanagementsystem httpwebrequest image index input install java label list listbox localization login mandelbrot math messagebox mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox server sleep socket sql statistics stream string stringformatting sun table text textbox thread time timer update usercontrol validation visualstudio webbrowser whileloop windows winforms wpf xml



