public SingleInstance(MainForm form, string[] args, string currentMutexName) { try { string name = currentMutexName; if (Program.ApplicationMutex != null) { MainForm.MainFormMutex = TheMutex = Program.ApplicationMutex; First = true; } else { MainForm.MainFormMutex = TheMutex = new Mutex(true, name, out First); } string objectName = "iRobotKinectSingleInstanceProxy"; string objectUri = "ipc://" + name + "/" + objectName; // NGE07252013 string result = ""; // NGE07252013 if (args != null) // NGE07252013 { // NGE07252013 if (First) // NGE07252013 result += "First\t" + string.Join("\t", args); // NGE07252013 else // NGE07252013 result += "Second\t" + string.Join("\t", args); // NGE07252013 } if (First) { // NGE07252013 MessageBox.Show(result); IpcChannel = new IpcServerChannel(name); ChannelServices.RegisterChannel(IpcChannel, false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(IpcObject), objectName, WellKnownObjectMode.Singleton); IpcObject obj = new IpcObject(new NewInstanceHandler(form.SecondInstanceStarted)); RemotingServices.Marshal(obj, objectName); } else { // NGE07252013 MessageBox.Show(result); IpcChannel = new IpcClientChannel(); ChannelServices.RegisterChannel(IpcChannel, false); IpcObject obj = Activator.GetObject(typeof(IpcObject), objectUri) as IpcObject; obj.SignalNewInstance(args); } } catch (Exception ex) { string exMessage = ex.Message; } }
public void InitAll() { CRMainForm = this; InitializeComponent(); Init(); }
static void Main(string[] args) { UnhandledExceptionManager.AddHandler(); // This allows ability to catch all unhandled exceptions in the program! Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //create parser CMDLineParser parser = new CMDLineParser(); parser.throwInvalidOptionsException = false; //add Options #region addOptions //add default help "-help",.. parser.AddHelpOption(); CMDLineParser.Option TouchInputFilenameOpt = parser.AddStringParameter("-l", "Left", false); CMDLineParser.Option TouchInputDirectoryOpt = parser.AddStringParameter("-r", "Right", false); CMDLineParser.Option TouchOutputFilenameOpt = parser.AddStringParameter("-f", "Forwards", false); CMDLineParser.Option TouchOutputReportFileOpt = parser.AddStringParameter("-b", "Backwards", false); #endregion if (args.Length > 0) { try { parser.Parse(args); } catch (CMDLineParser.CMDLineParserException e) { Console.WriteLine("Error: " + e.Message); parser.HelpMessage(); } //parser.Debug(); #region Section dealing with Multiple Instances of iRobotKinect if (ApplicationMutex != null && MainForm.MainFormMutex == ApplicationMutex) // This is sent to us by another copy of the program! Assume it is the qlp file to load { MainForm.CRMainForm.TopMost = true; MessageBox.Show("Args passed to iRobotKinect by another instance are:" + args[0], "iRobotKinect Remote Message"); // NGE07252013 if (args != null) { if (args.Count() == 1) { MainForm.CRMainForm.CommandLineDrivenOpenPlate(args[0]); } else if (parser._matchedSwitches.Count == 0 && parser._unmatchedArgs.Count == 2 && System.IO.Path.GetFileName((string)args[0]) == "iRobotKinect.exe") // Assume it is the file to load { MainForm.CRMainForm.CommandLineDrivenOpenPlate(args[1]); } } #endregion Section dealing with Multiple Instances of iRobotKinect return; } Application.EnableVisualStyles(); // NGE07242014 // Changes default way buttons and controls are shown - commented out makes basic 3d button shading Application.SetCompatibleTextRenderingDefault(false); // NGE07252013 if (args != null) // NGE07252013 MessageBox.Show("A Application.Run(CRMainForm = new MainForm(args, parser))" + string.Join("\t", args)); // NGE07252013 else // NGE07252013 MessageBox.Show("A Application.Run(CRMainForm = new MainForm(args, parser))"); CRMainForm = new MainForm(); Program.Menu_Cache.Add(CRMainForm.Handle); Application.Run(CRMainForm); } else { #region Section dealing with Multiple Instances of iRobotKinect if (MainForm.AllowForMultipleInstancesOfiRobotKinect == false) { bool MutexOwner = false; // This application wants initial ownership of the "iRobotKinect", if not, then iRobotKinect is already running ApplicationMutex = new Mutex(true, DefaultMutexName, out MutexOwner); int tryNumber = 0; while (MutexOwner == false && tryNumber < 5) { Thread.Sleep(3000); // Sleep for 3 seconds to see if other instance has finished exiting // Try 5 times, waiting 3 seconds between each try ApplicationMutex = new Mutex(true, DefaultMutexName, out MutexOwner); tryNumber++; } if (MutexOwner == false) { MessageBox.Show("iRobotKinect software is already running.", "Error - Unable To Run iRobotKinect", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { bool TooManyInstancesRunning = false; string currentMutexName = "", newMutexName = ""; if (RuniRobotKinectDueToMutexCondition(true, out TooManyInstancesRunning, ref currentMutexName, ref newMutexName) == false) return; string mutexName = ""; bool bRet = IsiRobotKinectAlreadyRunning(ref mutexName); bool MutexOwner = false; if (newMutexName == "" || currentMutexName == mutexName) // The initial instance of iRobotKinect { if (newMutexName == "") MainForm.InitialInstanceMutexName = currentMutexName; ApplicationMutex = new Mutex(true, currentMutexName, out MutexOwner); } else ApplicationMutex = null; } #endregion Section dealing with Multiple Instances of iRobotKinect Application.EnableVisualStyles(); // NGE07242014 // Changes default way buttons and controls are shown - commented out makes basic 3d button shading Application.SetCompatibleTextRenderingDefault(false); SetMessageFiltering(); SplashCurrentlyShown = false; // NGE12262011 Set to True if showing Splash Screen // Show the splash ASAP // NGE11202013 SplashScreen.ShowSplash(); try { CRMainForm = new MainForm(); Program.Menu_Cache.Add(CRMainForm.Handle); Application.Run(CRMainForm); } catch (System.IO.FileNotFoundException excep) { MessageBox.Show("Missing file is : " + excep.FileName); } //catch (Exception ex) //{ // string errorString = ex.Message; // if (MainForm.ShowApplication) MessageBox.Show(errorString); //} } }