SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [STAThread]
  2.     static void Main()
  3.     {
  4.         bool mutexCreated=true;
  5.  
  6.         using (Mutex mutex = new Mutex(true, "eCS", out mutexCreated))
  7.         {
  8.             if (mutexCreated)
  9.             {
  10.                 Application.EnableVisualStyles();
  11.                 Application.SetCompatibleTextRenderingDefault(false);
  12.                 Application.Run(new Login());
  13.             }
  14.             else
  15.             {
  16.                 Process current = Process.GetCurrentProcess();
  17.                 foreach (Process process in Process.GetProcessesByName(current.ProcessName))
  18.                 {
  19.                     if (process.Id != current.Id)
  20.                     {
  21.                         XtraMessageBox.Show("Another instance of eCS is already running.", "eCS already running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  22.                         SetForegroundWindow(process.MainWindowHandle);
  23.                         break;
  24.                     }
  25.                 }
  26.             }
  27.         }
  28.     }
  29.    
  30. namespace CSMutex
  31. {
  32.     static class Program
  33.     {
  34.         [STAThread]
  35.         static void Main()
  36.         {
  37.             bool mutexCreated=true;
  38.             using(Mutex mutex = new Mutex(true, "eCS", out mutexCreated))
  39.             {
  40.                 if (mutexCreated)
  41.                 {
  42.                     Application.EnableVisualStyles();
  43.                     Application.SetCompatibleTextRenderingDefault(false);
  44.                     Login loging = new Login();
  45.                     Application.Run(loging);
  46.                     Application.Run(new Main() { UserName = loging.UserName });
  47.                 }
  48.                 else
  49.                 {
  50.                     Process current = Process.GetCurrentProcess();
  51.                     foreach (Process process in Process.GetProcessesByName(current.ProcessName))
  52.                     {
  53.                         if (process.Id != current.Id)
  54.                         {
  55.                             MessageBox.Show("Another instance of eCS is already running.", "eCS already running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  56.                             //SetForegroundWindow(process.MainWindowHandle);
  57.                             break;
  58.                         }
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.     }
  64. }
  65.    
  66. public static class LoginForm
  67. {
  68.     private static Form _loginForm = new Form();
  69.  
  70.  
  71.     public static bool ShowLoginForm(){
  72.  
  73.         if(_loginForm.Visible)
  74.              return false;
  75.  
  76.         _loginForm.Show();
  77.         return true;
  78.     }
  79. }
  80.    
  81. [STAThread]
  82.     static void Main(string[] args)
  83.     {
  84.         using (System.Threading.Mutex mutex = new System.Threading.Mutex(true, "MyApp.SingleInstance.Mutex", out createdNew))
  85.         {
  86.             MainForm = new MainDlg();
  87.             SingleInstanceApplication.Run(MainForm, StartupNextInstanceEventHandler);
  88.         }
  89.     }
  90.  
  91.     public static void StartupNextInstanceEventHandler(object sender, StartupNextInstanceEventArgs e)
  92.     {
  93.         MainForm.Activate();
  94.     }
  95.  
  96. public class SingleInstanceApplication : WindowsFormsApplicationBase
  97. {
  98.     private SingleInstanceApplication()
  99.     {
  100.         base.IsSingleInstance = true;
  101.     }
  102.  
  103.     public static void Run(Form f, StartupNextInstanceEventHandler startupHandler)
  104.     {
  105.         SingleInstanceApplication app = new SingleInstanceApplication();
  106.         app.MainForm = f;
  107.         app.StartupNextInstance += startupHandler;
  108.         app.Run(Environment.GetCommandLineArgs());
  109.     }
  110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top