| | |
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 array asp barchart bitmap box broadcast buttons c# check checkbox client column combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development display draganddrop drawing encryption enum equation event excel file form format formbox forms formupdate function gdi+ httpwebrequest image index input install java label linux list listbox mandelbrot math mouseclick mysql networking operator packaging parse path photoshop picturebox pixelinversion post powerpacks programming radians regex remote remoting reporting richtextbox robot server sleep socket sql statistics stream string table text textbox thread time timer transform treeview update usercontrol validation visualstudio webbrowser wfa windows winforms wpf xml



