Beispiel #1
0
        protected void InitializeApp()
        {
            string[]      args      = Environment.GetCommandLineArgs();
            List <string> fileTypes = FileTypes.ToList();

            foreach (string arg in args)
            {
                if (arg == "-d" || arg == "--debug")
                {
                    DebugMode = true;
                    StartupArguments.Add(arg);
                }
                else if (arg.StartsWith("-") || arg.StartsWith("--"))
                {
                    StartupArguments.Add(arg);
                }
                else
                {
                    FileInfo file = new FileInfo(arg);
                    if (file.Exists && fileTypes.Contains(file.Extension))
                    {
                        StartupFileNames.Add(arg);
                    }
                }
            }

#if DEBUG
            DebugMode = true;
#endif

            bool  newMutex;
            Mutex mutex = new Mutex(true, AppId, out newMutex);

            if (!newMutex)
            {
                if (SingleInstanceApplication == true)
                {
                    using (IPCNamedPipeClient client = new IPCNamedPipeClient(AppId))
                    {
                        IPCMessage message = new IPCMessage();
                        message.Type = IPCMessageType.BringToFront;
                        client.SendMessage(message);
                    }
                    Current.Shutdown();
                    return;
                }
                else if (OpenDocsInFirstInstance && StartupFileNames.Count > 0)
                {
                    using (IPCNamedPipeClient client = new IPCNamedPipeClient(AppId))
                    {
                        IPCMessage message = new IPCMessage();
                        message.Type = IPCMessageType.OpenDocument;
                        message.Data = StartupFileNames;
                        client.SendMessage(message);
                    }
                    Current.Shutdown();
                }

                GainMutexTask = new Task(() => {
                    try
                    {
                        bool isNewMutex = false;
                        while (isNewMutex == false && !AbortMutex)
                        {
                            Thread.Sleep(100);
                            mutex = new Mutex(true, AppId, out isNewMutex);
                            if (isNewMutex)
                            {
                                s_mutex = mutex;
                            }
                            else
                            {
                                mutex.Close();
                                mutex.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogException(ex, ex.Message, false);
                    }
                    HasMutex = true;
                });

                GainMutexTask.Start();
            }
            else
            {
                s_mutex  = mutex;
                HasMutex = true;
            }

            Dispatcher.UnhandledException += Dispatcher_UnhandledException;
            if (UseDefaultMessageBox)
            {
                LogHelper.OnMessageBox += LogHelper_OnMessageBox;
            }

            if (ShutdownOnFatalError)
            {
                LogHelper.OnFatalError += LogHelper_OnFatalError;
            }
            LogHelper.LogMessage(LogLevel.Info, string.Format("Started Application {0}", Assembly.GetExecutingAssembly().FullName));
            Exit += wpfApp_Exit;
        }