SingleInstance

Discussion in 'Microsoft Dot NET Framework Forms' started by Vivek Waghmare, Mar 26, 2009.

  1. 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 Waghmare, Mar 26, 2009
    #1
    1. Advertisements

  2. Vivek Waghmare wrote:
    > 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.

    --
    Arto Viitanen
     
    Arto Viitanen, Mar 27, 2009
    #2
    1. Advertisements

  3. 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.
    "Arto Viitanen" <> wrote in message
    news:...
    > Vivek Waghmare wrote:
    >> 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.
    >
    > --
    > Arto Viitanen
     
    Vivek Waghmare, Mar 27, 2009
    #3
  4. Vivek Waghmare

    Phil Wilson Guest

    I think you might be making assumptions about those BringToFront/Foreground
    calls, that you can force some app window to be on top of everything else,
    but you can't:

    http://blogs.msdn.com/oldnewthing/archive/2009/02/20/9435239.aspx

    --
    Phil Wilson
    Definitive Guide to Windows Installer
    http://www.apress.com/book/view/1590592972


    "Vivek Waghmare" <> wrote in message
    news:...
    > 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.
    > "Arto Viitanen" <> wrote in message
    > news:...
    >> Vivek Waghmare wrote:
    >>> 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.
    >>
    >> --
    >> Arto Viitanen

    >
    >
     
    Phil Wilson, Mar 31, 2009
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page