Example #1
0
        private static void ExecuteThread()
        {
            Debug.WriteLine("\n*** Named pipe server stream starting (" + Program.PROCESS_PIPE_NAME + ") ***\n");

            var pipeServer = new NamedPipeServerStream(Program.PROCESS_PIPE_NAME, PipeDirection.InOut, 1);
            int threadId   = Thread.CurrentThread.ManagedThreadId;

            while (run)
            {
                pipeServer.WaitForConnection();

                try
                {
                    StreamString ss       = new StreamString(pipeServer);
                    var          incoming = ss.ReadString();

                    Debug.WriteLine("Incoming command from pipe: " + incoming);
                    ss.WriteString("OK");
                    pipeServer.Disconnect();

                    if (incoming.Length <= 0)
                    {
                        continue;
                    }

                    string[] commandParts = incoming.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    CommandProcessor.Process(commandParts);
                }
                catch (IOException e)
                {
                    Debug.WriteLine("\n*** ERROR - Named pipe server stream failed, exception details follow [will try to continue] (" + Program.PROCESS_PIPE_NAME + ") ***\n\n {0} \n\n", e.Message);

                    try
                    {
                        pipeServer.Disconnect();
                    }
                    catch (Exception) { }
                }
            }
        }
Example #2
0
        internal static void Main(string[] args)
        {
            var joinedArgs = String.Join(" ", args);

            Debug.WriteLine("MultiClip " + Assembly.GetExecutingAssembly().GetName().Version.ToString());

            if (!String.IsNullOrWhiteSpace(joinedArgs))
            {
                Debug.WriteLine("Invoked with command line parameters: " + joinedArgs);
            }

            // If we can not aquire a mutex lock (i.e. one already exists), that means we need
            // to send the command to the existing process.
            if (!EnsureMainInstance())
            {
                Debug.WriteLine("WARNING: Could not aquire a process mutex lock!");

                if (!String.IsNullOrWhiteSpace(joinedArgs))
                {
                    var client = new PipeClient();
                    client.Open();
                    client.SendAndRead(joinedArgs);
                    client.Close();
                }
                else
                {
                    MessageBox.Show("MultiClip is already active in the background.", "You tried to start MultiClip", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                Debug.WriteLine("This process has completed its work. Farewell, cruel world!");

                Environment.Exit(0);
                return;
            }

            Debug.WriteLine("Successfully aquired a mutex lock for MultiClip (" + PROCESS_MUTEX_NAME + "); this is the main process.");

            // Initialize server
            PipeServer.Start();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Initialize tray
            tray              = new NotifyIcon();
            tray.Icon         = Icon.FromHandle(Resources.iconWhite.GetHicon());
            tray.Text         = "MultiClip";
            tray.Visible      = true;
            tray.DoubleClick += tray_DoubleClick;

            // Set up tray context menu
            tray.ContextMenu = new ContextMenu();
            tray.ContextMenu.MenuItems.Add("Clipboard 1", new EventHandler(OnClipClicked));
            tray.ContextMenu.MenuItems.Add("Clipboard 2", new EventHandler(OnClipClicked));
            tray.ContextMenu.MenuItems.Add("Clipboard 3", new EventHandler(OnClipClicked));
            tray.ContextMenu.MenuItems.Add("Clipboard 4", new EventHandler(OnClipClicked));
            tray.ContextMenu.MenuItems.Add("Clipboard 5", new EventHandler(OnClipClicked));
            tray.ContextMenu.MenuItems.Add("-");
            tray.ContextMenu.MenuItems.Add("Exit", new EventHandler(OnExitClicked));

            CommandProcessor.Process(args);

            if (!Clipper.WasConfigured)
            {
                Clipper.SetClipboard(0);
            }

            Application.ApplicationExit += Application_ApplicationExit;
            Application.Run();
        }