I am working on a multi-user application. Is there a way in C # to know how many instances are currently running.I used one piece of code to count the window processes of the name of my application, but this is not very good.
string fileName = Process.GetCurrentProcess().MainModule.FileName; int count = 0; foreach (Process p in Process.GetProcesses()) { try { if (p.MainModule.FileName == fileName) { count++; } } catch { } } MessageBox.Show("Total Instances Running are " + count);
This can be done using a semaphore or increment and decrement counter, which increases by one when creating a new instance and decreases by one when closing the instance.
A Semaphore , , 0. , .
Semaphore
, , , , .
? , , , .
, P/Invoke, , .
. , - . , , , . AppDomains .NET-, , , , .
, , . , - , . - , , , . - , , .
, Process.Exited . .