SingleInstance

  • Thread starter Thread starter Vivek Waghmare
  • Start date Start date
V

Vivek Waghmare

Hi
My purpose to create an application which should not allowed to run more
than one instance of it.

I have acheived this by doing following steps

1. Created a class named SingleInstance inherhated from
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
2. override method OnCreateMainForm
this.MainForm = new MainForm();

if(this.CommandLineArgs != null && this.CommandLineArgs.Count > 0)

{

((MainForm)this.MainForm).ProcessArguments(this.CommandLineArgs);

( (MainForm) this.MainForm ).BringToFront();

}

3. overrdie method OnStatupNextInstance
protected override void
OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs
eventArgs)

{

base.OnStartupNextInstance(eventArgs);

eventArgs.BringToForeground = true;

try

{

this.OpenForms["CustomizationWizardForm"].Activate();

}

catch

{

//This try-catch block is used to avoid exception that will arise when one
instacne is running and

//user clicks on another FR project or same exe

}

if(eventArgs.CommandLine != null && eventArgs.CommandLine.Count > 0)

{

if(this.MainForm != null)

{

( (MainForm) this.MainForm ).ProcessArguments(eventArgs.CommandLine);

}

}

}

4. In Main method

created instance of SingleInstance



SingleInstance singleInstance = new SingleInstance();

singleInstance.Run(args);



I am getting my required result, Any new instance of my application is now
presvented.

But i am facing a strange problems

When launched my application does not get focus. Its maxmized but without
focus, and it starts blinking in orange in taskbar.



Please help
 
Vivek said:
Hi
My purpose to create an application which should not allowed to run more
than one instance of it.

I have acheived this by doing following steps

1. Created a class named SingleInstance inherhated from
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
2. override method OnCreateMainForm

You can use System.Threading.Mutex. Following code:

bool createdNew;
string mutexName = "Ourprogramname";
Mutex mutex = new Mutex(false, mutexName, out createdNew);
if (!createdNew)
{
MessageBox.Show("There can be only one", "Oops");
Application.Exit();
return;
}

lets only one intance of the program to be run; actually second instance
stops.
 
Thanks for your input

But this is not my problem. I have acheived of maintaining single instance
of application. I can create

My issue is that our application is not getting activited when it starts.
It stays minimize at taskbar blinking in orange.
 
Back
Top