Solved

C# Detect for start application twice time (advanced)

Posted on 2009-07-02
4
892 Views
Last Modified: 2013-12-17
Hi experts!

  Im using C# 2008 , WinForm NET 2. I have application, which if is started with command parameters start different forms (and this form stay open all the time and can not be change).
I want to make sure that this application can not be started twice on 1 computer. This is simple, but not in this case. Because in fact application is count if is started twice with same parametyr. (possible option are only 3 and are static text like param1 or param2 or param3 How to do this ?

 In COM world I will ask and get for caption of application and will understand have or not have already started this application. But how I can do this via NET?

 I can save let say in registry this info, but if my application fail it will not save that is close and next time is possible to be unable to start J

 I need this  to keep from people error with started twice time my application.
0
Comment
Question by:dvplayltd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 24770285
Ensuring that only a single instance of a .NET application is running
http://www.ai.uga.edu/~mc/SingleInstance.html

[STAThread]
    static void Main()                  // args are OK here, of course
    {
        bool ok;
        m = new System.Threading.Mutex(true, "YourNameHere", out ok);
 
        if (! ok)
        {
            MessageBox.Show("Another instance is already running.");
            return;
        }
 
        Application.Run(new Form1());   // or whatever was there
 
        GC.KeepAlive(m);                // important!
    }

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24770350
0
 
LVL 9

Expert Comment

by:Rahul Goel ITIL
ID: 24770497
Hey this will help you.
http://en.csharp-online.net/Application_Architecture_in_Windows_Forms_2.0%E2%80%94Single-Instance_Detection_and_Management

you can use WindowsFormsApplicationBase class and check whether you appl has got different forms or not
0
 
LVL 2

Expert Comment

by:retorikci
ID: 24770924
in your Program.cs try this code.
in this example we avoid Login twice times.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices; 
 
    //static class Program
    //{
    //    /// <summary>
    //    /// The main entry point for the application.
    //    /// </summary>
    //    [MTAThread]
    //    static void Main()
    //    {
    //        try
    //        {
    //            Application.Run(new frmLogin());
    //        }
    //        catch (Exception ex)
    //        {
    //            MessageBox.Show(ex.ToString());
    //        }
    //    }
    //}
 
    static class Program
    {
        // need this for single instance detection 
        [DllImport("coredll.dll")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("coredll.dll")]
        private static extern bool SetForegroundWindow(int hwnd);
 
        const string appName = "Login";
        const int ALREADY_EXISTS = 183;
        
        static void Main()
        {
            startTicks = Environment.TickCount;
 
            using (AppExecutionManager execMgr = new AppExecutionManager(appName))
            {
                if (execMgr.IsFirstInstance)
                    Application.Run(new frmLogin());
                else
                {
                    int hWnd = FindWindow(null, appName);
                    SetForegroundWindow(hWnd); 
                }
            }
        }
        internal static int startTicks;
    }

Open in new window

0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query Help Top 1 and Distinct? 6 49
how to just get time from a date 6 48
Adding items to a C# list incrementally 5 64
VB.net Filesystem watcher not working 5 38
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question