예제 #1
0
        public bool ProcessMessages(NamedPipeServerStream server)
        {
            try
            {
                BinaryFormatter binForm = new BinaryFormatter();
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    server.CopyTo(memoryStream);
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    object data = binForm.Deserialize(memoryStream);
                    Application.Current.Dispatcher.InvokeAsync(() =>
                    {
                        string message = data as string;
                        if (message != null)
                        {
                            switch (message)
                            {
                                case "MainWindow":
                                    {

                                        foreach (Window win in Application.Current.Windows)
                                        {
                                            if (win.GetType() == typeof(MainWindow))
                                            {
                                                win.Activate();
                                                return;
                                            }
                                        }
                                        MainWindow mw = new MainWindow();
                                        mw.Show();
                                        mw.Activate();
                                        break;
                                    }
                                case "Exit":
                                    {
                                        Application.Current.Shutdown();
                                        break;
                                    }
                            }
                        }
                        else
                        {
                            var newGesture = data as Tuple<string, List<List<List<Point>>>>;
                            if (newGesture == null) return;

                            GestureDefinition gu = new GestureDefinition(new Gesture(newGesture.Item1, newGesture.Item2.Select(list => new PointPattern(list)).ToArray()), false);
                            gu.Show();
                            gu.Activate();
                        }
                    }, DispatcherPriority.Input);
                }
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
        }
예제 #2
0
        private async void Application_Startup(object sender, StartupEventArgs e)
        {
            try
            {
                Logging.OpenLogFile();
                LoadLanguageData();

                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GestureSignDaemon.exe");
                if (!File.Exists(path))
                {
                    MessageBox.Show(LocalizationProvider.Instance.GetTextValue("Messages.CannotFindDaemonMessage"),
                        LocalizationProvider.Instance.GetTextValue("Messages.Error"), MessageBoxButton.OK, MessageBoxImage.Error);
                    Current.Shutdown();
                    return;
                }

                if (CheckIfApplicationRunAsAdmin())
                {
                    var result = MessageBox.Show(LocalizationProvider.Instance.GetTextValue("Messages.CompatWarning"),
                     LocalizationProvider.Instance.GetTextValue("Messages.CompatWarningTitle"), MessageBoxButton.YesNo,
                     MessageBoxImage.Warning);

                    if (result == MessageBoxResult.No)
                    {
                        Current.Shutdown();
                        return;
                    }
                }

                bool createdNewDaemon;
                using (new Mutex(false, "GestureSignDaemon", out createdNewDaemon))
                {
                }
                if (createdNewDaemon)
                {
                    using (Process daemon = new Process())
                    {
                        daemon.StartInfo.FileName = path;

                        //daemon.StartInfo.UseShellExecute = false;
                        if (IsAdministrator())
                            daemon.StartInfo.Verb = "runas";
                        daemon.StartInfo.CreateNoWindow = false;
                        daemon.Start();
                    }
                    Current.Shutdown();
                }
                else
                {
                    bool createdNew;
                    mutex = new Mutex(true, "GestureSignControlPanel", out createdNew);
                    if (createdNew)
                    {
                        var systemAccent = UIHelper.GetSystemAccent();
                        if (systemAccent != null)
                        {
                            var accent = ThemeManager.GetAccent(systemAccent);
                            ThemeManager.ChangeAppStyle(Current, accent, ThemeManager.GetAppTheme("BaseLight"));
                        }

                        if (e.Args.Length != 0 && e.Args[0].Equals("/L"))
                        {
                            Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                            Timer.Change(300000, Timeout.Infinite);
                        }
                        else
                        {
                            MainWindow mainWindow = new MainWindow();
                            mainWindow.Show();
                        }

                        GestureManager.Instance.Load(null);
                        PluginManager.Instance.Load(null);
                        ApplicationManager.Instance.Load(null);

                        NamedPipe.Instance.RunNamedPipeServer("GestureSignControlPanel", new MessageProcessor());
                    }
                    else
                    {
                        await NamedPipe.SendMessageAsync("MainWindow", "GestureSignControlPanel");
                        Current.Shutdown();
                    }

                }

            }
            catch (Exception exception)
            {
                Logging.LogException(exception);
                MessageBox.Show(exception.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                Current.Shutdown();
            }

        }