Ejemplo n.º 1
0
        private static void OnCheckFileOpen(AppInstanceInfo sender, string data)
        {
            bool isFileOpen = MainWindow?.IsFileOpen(data) ?? false;

            Trace.WriteLine($"PID: {Current.ProcessID} IsFileOpen('{data}') => {isFileOpen}");
            SendAction(sender, MSG_RETURN_FILE_OPEN, isFileOpen.ToString());
        }
Ejemplo n.º 2
0
        public static void Initialize(BrickEditorWindow mainWindow)
        {
            var myProc = Process.GetCurrentProcess();

            ProcessName = myProc.ProcessName;

            Current = new AppInstanceInfo()
            {
                WindowHandle = myProc.MainWindowHandle,
                ProcessID    = myProc.Id,
                InstanceID   = Guid.NewGuid().ToString("N")
            };

            MainWindow = mainWindow;

            if (Current.WindowHandle == IntPtr.Zero)
            {
                Current.WindowHandle = mainWindow.Handle;
            }

            MessageHandlers.Add(MSG_NOTIFY, OnNotify);
            MessageHandlers.Add(MSG_QUERY_FILE_OPEN, OnCheckFileOpen);


            PollActiveInstances();
        }
Ejemplo n.º 3
0
        public static void PollActiveInstances()
        {
            AppInstances.Clear();
            var otherProcs = Process.GetProcessesByName(ProcessName);

            if (otherProcs.Length > 1)
            {
                foreach (var proc in otherProcs)
                {
                    if (proc.Id == Current.ProcessID)
                    {
                        continue;
                    }

                    var instanceInfo = new AppInstanceInfo()
                    {
                        ProcessID    = proc.Id,
                        WindowHandle = proc.MainWindowHandle
                    };
                    AppInstances.Add(instanceInfo);
                    SendAction(instanceInfo, MSG_NOTIFY, Current.ToString());
                    break;
                }
            }
        }
Ejemplo n.º 4
0
        private static void OnNotify(AppInstanceInfo sender, string data)
        {
            var instanceInfo = AppInstanceInfo.Parse(data);

            if (!AppInstances.Any(a => a.ProcessID == instanceInfo.ProcessID))
            {
                AppInstances.Add(instanceInfo);
            }
        }
Ejemplo n.º 5
0
        //private static async Task<string> AwaitResponse(string action)
        //{
        //    var mre = new ManualResetEvent(false);
        //    string actionResult = null;

        //    MessageHandlers.Add(action, (AppInstanceInfo sender, string msg) =>
        //    {
        //        mre.Set();
        //        actionResult = msg;
        //    });

        //    var waitTask = Task.Factory.StartNew(() =>
        //    {
        //        mre.WaitOne(2000);
        //        Trace.WriteLine("ManualResetEvent is set!");
        //    });

        //    await waitTask;

        //    mre.Dispose();
        //    MessageHandlers.Remove(action);
        //    return actionResult;
        //}

        private static void SendAction(AppInstanceInfo instance, string actionName, string message)
        {
            try
            {
                SendDataMessage(Current.WindowHandle, instance.WindowHandle, $"{MSG_HEADER}{actionName}#{message}");
            }
            catch
            {
                CheckIsActive(instance);
            }
        }
Ejemplo n.º 6
0
        private static bool CheckIsActive(AppInstanceInfo instance)
        {
            bool isActive = false;

            try
            {
                var instanceProc = Process.GetProcessById(instance.ProcessID);
                isActive = (instanceProc != null && instanceProc.ProcessName == ProcessName);
            }
            catch { }

            if (!isActive)
            {
                AppInstances.Remove(instance);
            }

            return(isActive);
        }
Ejemplo n.º 7
0
        private static string GetActionResult(AppInstanceInfo instance, string actionName, string resultName, string data)
        {
            var    mre          = new ManualResetEvent(false);
            string actionResult = null;

            MessageHandlers.Add(resultName, (AppInstanceInfo sender, string msg) =>
            {
                mre.Set();
                actionResult = msg;
            });

            SendAction(instance, actionName, data);

            mre.WaitOne(1500);
            mre.Dispose();
            MessageHandlers.Remove(resultName);

            return(actionResult);
        }