Check for Single-Instance

Please support our C# advertiser: Intel Parallel Studio Home
vckicks vckicks is offline Offline Aug 25th, 2008, 2:26 am |
1
A simple way to check if the current C# program is the only instance of itself running.
Quick reply to this message  
C# Syntax
  1. private bool IsSingleInstance()
  2. {
  3. foreach (Process process in Process.GetProcesses())
  4. {
  5. if (process.MainWindowTitle == this.Text)
  6. return false;
  7. }
  8. return true;
  9. }
0
ashishkumar008 ashishkumar008 is offline Offline | 23 Days Ago
this give error....
In foreach line (in process)
always mention namespace which is required and that place where code to be paste
 
1
gusano79 gusano79 is offline Offline | 23 Days Ago
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:

  1. private Mutex GetSingleInstanceMutex()
  2. {
  3. bool createdNewMutex;
  4.  
  5. Mutex mutex = new Mutex(false, @"Global\" + SomethingUnique, out createdNewMutex);
  6.  
  7. if (!createdNewMutex)
  8. {
  9. mutex = null;
  10. }
  11.  
  12. return mutex;
  13. }

SomethingUnique should be something unique to the application, for example, the assembly GUID.

Here's how you'd use the method:

  1. using (Mutex mutex = GetSingleInstanceMutex())
  2. {
  3. if (mutex != null)
  4. {
  5. // This is the only running instance; go ahead and run the application.
  6. }
  7. else
  8. {
  9. // Another instance is running; notify user and quit.
  10. }
  11. }
 
0
ashishkumar008 ashishkumar008 is offline Offline | 22 Days Ago
Request:-
i want to use flash file in webbrowser like other resources like picture
i do not want to use full path (c:\programfile\).
can u post full c# code
please
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC