Make only one instance of an application run with VB.NET
Takeaway: Irina Medvinskaya presents a way to allow only one instance of an application run with VB.NET. She presents a code example that uses the System.Diagnostics.Process class to determine whether there is more than one instance of an application running.
It's usually a good practice to make sure that only one instance of your application can be run at a time. In order to determine if there are other instances of an application running, you need to get a list of all the processes running and see if an instance of the application is there. In Listing A, I utilize the System.Diagnostics.Process class for this purpose.
In the example, I define an array, appProc, as a Process. Then I define two string variables: strModName and strProcName. The value of variable strModName is set to the Process.GetCurrentProcess.MainModule.ModuleName property, and the value of variable strProcName is set to the System.IO.Path.GetFileNameWithoutExtension, to which I pass the name of the module in strModName. I set the value of the array appProc to the value of Process.GetProcessesByName and pass the strProcName to it. Then, I simply check the Length property of the array appProc. If the length is more than one, it means that there is more then one instance of the application running, which is what I use to base the displayed message box on.
Notes about the example
You should use the example as a part of the Main routine of an application. If you determine that there are other instances of this application running, you can decide on the appropriate action; in most cases, alerting a user to the fact that the application is already running will be enough. If there are no other instances of the application running, you can proceed with the necessary application logic.
Miss a tip?
Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!
Print/View all Posts Comments on this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- Addressing Compliance Requirements for Privacy, Data Retention, and e-Discovery Trend Micro Protecting individual and financial data, retaining data, and meeting ... Download Now
- Active Administrator Version 5.1 ScriptLogic Active Administrator? is an enterprise-class Active Directory management ... Download Now
- Dell Case Study: Bergstrasse District Council Dell The Bergstrasse district in Germany wanted to provide each pupil with a ... Download Now
- Cost-Effective IT Platform Is a Welcome Guest at Upscale and Luxury Hotel Chain Dell Wyndham International is a hotel chain renowned for offering personalized ... Download Now
- July 14th: The Virtual Presenter's Series: Start Right! Design Right! Present Right! Citrix Online Presenting online is a completely different ballgame from presenting in ... Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET





