Ejemplo n.º 1
0
        private static void Sub_Main(string[] orgArgs)
        {
            AppDomain.CurrentDomain.UnhandledException +=
                new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            Logger.logInfo("============================================================================");
            Logger.logInfo("LogExpert " + Assembly.GetExecutingAssembly().GetName().Version.Major + "." +
                           Assembly.GetExecutingAssembly().GetName().Version.Minor + "/" +
                           Assembly.GetExecutingAssembly().GetName().Version.Build.ToString() +
                           " started.");
            Logger.logInfo("============================================================================");

            CmdLine       cmdLine    = new CmdLine();
            CmdLineString configFile = new CmdLineString("config", false, "A configuration (settings) file");

            cmdLine.RegisterParameter(configFile);
            string[] remainingArgs = cmdLine.Parse(orgArgs);

            List <string> argsList = new List <string>();

            // This loop tries to convert relative file names into absolute file names (assuming that platform file names are given).
            // It tolerates errors, to give file system plugins (e.g. sftp) a change later.
            // TODO: possibly should be moved to LocalFileSystem plugin
            foreach (string fileArg in remainingArgs)
            {
                try
                {
                    FileInfo info = new FileInfo(fileArg);
                    if (info.Exists)
                    {
                        argsList.Add(info.FullName);
                    }
                    else
                    {
                        argsList.Add(fileArg);
                    }
                }
                catch (Exception)
                {
                    argsList.Add(fileArg);
                }
            }
            string[] args = argsList.ToArray();
            if (configFile.Exists)
            {
                FileInfo cfgFileInfo = new FileInfo(configFile.Value);
                if (cfgFileInfo.Exists)
                {
                    ConfigManager.Import(cfgFileInfo, ExportImportFlags.All);
                }
                else
                {
                    MessageBox.Show("Config file not found", "LogExpert");
                }
            }

            int pId = Process.GetCurrentProcess().SessionId;

            try
            {
                Settings settings  = ConfigManager.Settings;
                bool     isCreated = false;
                Mutex    mutex     = new System.Threading.Mutex(false, "Local\\LogExpertInstanceMutex" + pId, out isCreated);
                if (isCreated)
                {
                    // first application instance
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    LogTabWindow logWin = new LogTabWindow(args.Length > 0 ? args : null, 1, false);

                    // first instance
                    //WindowsIdentity wi = WindowsIdentity.GetCurrent();
                    IpcServerChannel ipcChannel = new IpcServerChannel("LogExpert" + pId);
                    ChannelServices.RegisterChannel(ipcChannel, false);
                    RemotingConfiguration.RegisterWellKnownServiceType(typeof(LogExpertProxy),
                                                                       "LogExpertProxy",
                                                                       WellKnownObjectMode.Singleton);
                    LogExpertProxy proxy = new LogExpertProxy(logWin);
                    RemotingServices.Marshal(proxy, "LogExpertProxy");

                    LogExpertApplicationContext context = new LogExpertApplicationContext(proxy, logWin);
                    Application.Run(context);

                    ChannelServices.UnregisterChannel(ipcChannel);
                }
                else
                {
                    int              counter    = 3;
                    string           errMsg     = "";
                    IpcClientChannel ipcChannel = new IpcClientChannel("LogExpertClient#" + pId, null);
                    ChannelServices.RegisterChannel(ipcChannel, false);
                    while (counter > 0)
                    {
                        try
                        {
                            // another instance already exists
                            //WindowsIdentity wi = WindowsIdentity.GetCurrent();
                            LogExpertProxy proxy = (LogExpertProxy)Activator.GetObject(typeof(LogExpertProxy),
                                                                                       "ipc://LogExpert" + pId + "/LogExpertProxy");
                            if (settings.preferences.allowOnlyOneInstance)
                            {
                                proxy.LoadFiles(args);
                            }
                            else
                            {
                                proxy.NewWindowOrLockedWindow(args);
                            }
                            break;
                        }
                        catch (RemotingException e)
                        {
                            Logger.logError("IpcClientChannel error: " + e.Message);
                            errMsg = e.Message;
                            counter--;
                            Thread.Sleep(500);
                        }
                    }
                    if (counter == 0)
                    {
                        Logger.logError("IpcClientChannel error, giving up: " + errMsg);
                        MessageBox.Show("Cannot open connection to first instance (" + errMsg + ")", "LogExpert");
                    }
                }
                mutex.Close();
            }
            catch (Exception ex)
            {
                Logger.logError("Mutex error, giving up: " + ex.Message);
                MessageBox.Show("Cannot open connection to first instance (" + ex.Message + ")", "LogExpert");
            }
        }
Ejemplo n.º 2
0
        private static void Sub_Main(string[] orgArgs)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            Logger.logInfo("============================================================================");
            Logger.logInfo("LogExpert " + Assembly.GetExecutingAssembly().GetName().Version.Major + "." +
                           Assembly.GetExecutingAssembly().GetName().Version.Minor + "/" +
                           Assembly.GetExecutingAssembly().GetName().Version.Build.ToString() +
                           " started.");
            Logger.logInfo("============================================================================");

            List <string> argsList = new List <string>();

            foreach (string fileArg in orgArgs)
            {
                try
                {
                    FileInfo info = new FileInfo(fileArg);
                    if (info.Exists)
                    {
                        argsList.Add(info.FullName);
                    }
                    else
                    {
                        argsList.Add(fileArg);
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("File name " + fileArg + " is not a valid file name!", "LogExpert Error");
                }
            }
            string[] args = argsList.ToArray();

            int pId = Process.GetCurrentProcess().SessionId;

            try
            {
                Settings settings  = ConfigManager.Settings;
                bool     isCreated = false;
                Mutex    mutex     = new System.Threading.Mutex(false, "Local\\LogExpertInstanceMutex" + pId, out isCreated);
                if (isCreated)
                {
                    // first application instance
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    LogTabWindow logWin = new LogTabWindow(args.Length > 0 ? args : null, 1, false);

                    // first instance
                    //WindowsIdentity wi = WindowsIdentity.GetCurrent();
                    IpcServerChannel ipcChannel = new IpcServerChannel("LogExpert" + pId);
                    ChannelServices.RegisterChannel(ipcChannel, false);
                    RemotingConfiguration.RegisterWellKnownServiceType(typeof(LogExpertProxy),
                                                                       "LogExpertProxy",
                                                                       WellKnownObjectMode.Singleton);
                    LogExpertProxy proxy = new LogExpertProxy(logWin);
                    RemotingServices.Marshal(proxy, "LogExpertProxy");

                    LogExpertApplicationContext context = new LogExpertApplicationContext(proxy, logWin);
                    Application.Run(context);

                    ChannelServices.UnregisterChannel(ipcChannel);
                }
                else
                {
                    int              counter    = 3;
                    string           errMsg     = "";
                    IpcClientChannel ipcChannel = new IpcClientChannel("LogExpertClient#" + pId, null);
                    ChannelServices.RegisterChannel(ipcChannel, false);
                    while (counter > 0)
                    {
                        try
                        {
                            // another instance already exists
                            //WindowsIdentity wi = WindowsIdentity.GetCurrent();
                            LogExpertProxy proxy = (LogExpertProxy)Activator.GetObject(typeof(LogExpertProxy),
                                                                                       "ipc://LogExpert" + pId + "/LogExpertProxy");
                            if (settings.preferences.allowOnlyOneInstance)
                            {
                                proxy.LoadFiles(args);
                            }
                            else
                            {
                                proxy.NewWindowOrLockedWindow(args);
                            }
                            break;
                        }
                        catch (RemotingException e)
                        {
                            Logger.logError("IpcClientChannel error: " + e.Message);
                            errMsg = e.Message;
                            counter--;
                            Thread.Sleep(500);
                        }
                    }
                    if (counter == 0)
                    {
                        Logger.logError("IpcClientChannel error, giving up: " + errMsg);
                        MessageBox.Show("Cannot open connection to first instance (" + errMsg + ")", "LogExpert");
                    }
                }
                mutex.Close();
            }
            catch (Exception ex)
            {
                Logger.logError("Mutex error, giving up: " + ex.Message);
                MessageBox.Show("Cannot open connection to first instance (" + ex.Message + ")", "LogExpert");
            }
        }