Dotnetplace.com

.NET Articles, Web Devploment, ASP.NET, HTML, Sharepoint 2010, Aamir Hasan
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here

Advertise

Loading...


Posted by Aamir Hasan   on Tuesday, September 27, 2011 Total Views:  

In this article i have told you, how to stop running multiple instance of application in Visual Studio.NET using csharp and VB.NET. you can use Visual studio 2005/2008/2010. Here i have used Visual studio 2010.Let's start.


Open you Visual Studio 2010. Create a new project from Menu File->New project,  small window will be open,from installed template select Visual C# and then from right of this small window select Windows Form Application, give name and click OK button to continue. In the Solution Explorer Project has created as shown in Figure.1.1.

Figure 1.1

You have seen Form1 is already created when you have created a project. Open your Program.cs file. Replace your program.cs code with given below code.

CSharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Mutex//name space of your project
{
   static class Program
   {
       ///<summary>
       /// The main entry point for the application.
       ///</summary>
       [STAThread]
       staticvoid Main()
       {
           try

          {
               Boolean canRun = false;
               System.Threading.Mutex mutex = new System.Threading.Mutex(true, "Mutex", out canRun);
               if (canRun)
               {
                   Application.EnableVisualStyles();
                   Application.SetCompatibleTextRenderingDefault(false);
                   Application.Run(newForm1());
                   //--Tell the Garbage collector not to dispose this mutex
                   GC.KeepAlive(mutex);
               }
               else
               {
                   MessageBox.Show("Application is already running.", "My Application", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
           finally
           {
               //your logic
           }
       }
   }
}
   
VB.NET

Create a project in VB.Net. Right Click on your project from Solution Explorer select Application table and find “Window Application Framework properties” checked make single application  as shown in below figure 1.2

Figure 1.2

MSDN Link

Mutex Class

Download

Run Single instance vb.net.zip (63.33 kb)

Run Single instance csharp.zip (46.59 kb)

Category: All | Csharp
Protected by Copyscape Online Plagiarism Tool
Comments are closed
Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5