简体   繁体   中英

Check for Multiple Instances of a Visual Basic Application

How can I check for multiple instances of my visual basic application? When the user runs the application, I want to know how many instances are already open. Thank you.

The "fastest" way would be use to the System.Diagnostics.Process class to find all instances of your specific process name if all you want to do is get the count of processes.

So, as an example to find instances of "Explorer" use this.

Dim instanceCount As Integer = Process.GetProcessesByName("explorer").Count()
Console.WriteLine(String.Format("{0} Instances Running", instanceCount.ToString()))
Console.ReadLine()

If you are looking to limit users to 1 instance, then I would look at creating a named Mutex and using that to prevent others from starting.

Share a semaphore between the processes, and add a permit for every upstart. The most staight-forward way is probably to use a named semaphore. See the remarks section on http://msdn.microsoft.com/en-us/library/ms682438%28VS.85%29.aspx for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM