Exemplo n.º 1
0
        public bool RunInCurrentAppDomain(TransientSettingsData settingsData)
        {
            Trace.WriteLine($"Trying to run Snoop in app domain \"{AppDomain.CurrentDomain.FriendlyName}\"...");

            try
            {
                var instanceCreator = GetInstanceCreator(settingsData.StartTarget);

                var result = InjectSnoopIntoDispatchers(settingsData, (data, dispatcher) => CreateSnoopWindow(data, dispatcher, instanceCreator));

                Trace.WriteLine($"Successfully running Snoop in app domain \"{AppDomain.CurrentDomain.FriendlyName}\".");

                return(result);
            }
            catch (Exception exception)
            {
                Trace.WriteLine($"Failed to to run Snoop in app domain \"{AppDomain.CurrentDomain.FriendlyName}\".");

                if (SnoopModes.MultipleAppDomainMode)
                {
                    Trace.WriteLine($"Could not snoop a specific app domain with friendly name of \"{AppDomain.CurrentDomain.FriendlyName}\" in multiple app domain mode.");
                    Trace.WriteLine(exception);
                }
                else
                {
                    //ErrorDialog.ShowDialog(exception, "Error snooping", "There was an error snooping the application.", exceptionAlreadyHandled: true);
                }

                return(false);
            }
        }
Exemplo n.º 2
0
        private static InspectorWindow CreateSnoopWindow(TransientSettingsData settingsData, Dispatcher dispatcher, Func <InspectorWindow> instanceCreator)
        {
            var inspectorWinow = instanceCreator();


            ServiceLocator.RegisterInstance <InspectorWindow>(inspectorWinow);
            if (Application.Current != null && Application.Current.MainWindow != null)
            {
                inspectorWinow.Owner = Application.Current.MainWindow;
            }
            inspectorWinow.Show();
            return(inspectorWinow);
        }
Exemplo n.º 3
0
        public int StartInject(string p)
        {
            try
            {
                Debugger.Launch();
                var settingsData = TransientSettingsData.LoadCurrent(p);

                var succeeded = false;

                succeeded = this.RunInCurrentAppDomain(settingsData);

                return(0);
            }
            catch (Exception exception)
            {
                Trace.WriteLine(exception);
                return(1);
            }
        }
Exemplo n.º 4
0
 public DispatchOutParameters(TransientSettingsData settingsData, Func <TransientSettingsData, Dispatcher, InspectorWindow> instanceCreator, List <Visual> visuals)
 {
     this.SettingsData    = settingsData;
     this.InstanceCreator = instanceCreator;
     this.Visuals         = visuals;
 }
Exemplo n.º 5
0
        private static bool InjectSnoopIntoDispatchers(TransientSettingsData settingsData, Func <TransientSettingsData, Dispatcher, InspectorWindow> instanceCreator)
        {
            // check and see if any of the root visuals have a different mainDispatcher
            // if so, ask the user if they wish to enter multiple mainDispatcher mode.
            // if they do, launch a snoop ui for every additional mainDispatcher.
            // see http://snoopwpf.codeplex.com/workitem/6334 for more info.

            var rootVisuals = new List <Visual>();
            var dispatchers = new List <Dispatcher>();

            foreach (PresentationSource presentationSource in PresentationSource.CurrentSources)
            {
                var presentationSourceRootVisual = presentationSource.RootVisual;

                if (presentationSourceRootVisual is null)
                {
                    continue;
                }

                var presentationSourceRootVisualDispatcher = presentationSourceRootVisual.Dispatcher;

                // Check if we have already seen this dispatcher and it's root visual
                if (dispatchers.IndexOf(presentationSourceRootVisualDispatcher) == -1)
                {
                    dispatchers.Add(presentationSourceRootVisualDispatcher);

                    rootVisuals.Add(presentationSourceRootVisual);
                }
            }

            var useMultipleDispatcherMode = false;

            if (rootVisuals.Count > 0)
            {
                if (rootVisuals.Count == 1 ||
                    settingsData.MultipleDispatcherMode == MultipleDispatcherMode.NeverUse)
                {
                    var rootVisual = rootVisuals[0];

                    rootVisual.Dispatcher.Invoke((Action)(() =>
                    {
                        var snoopInstance = instanceCreator(settingsData, rootVisual.Dispatcher);

                        //if (snoopInstance.Target is null)
                        //{
                        //    snoopInstance.Target = rootVisual;
                        //}
                    }));

                    return(true);
                }

                // Should we skip the question and always use multiple dispatcher mode?
                if (settingsData.MultipleDispatcherMode == MultipleDispatcherMode.AlwaysUse)
                {
                    useMultipleDispatcherMode = true;
                }
                else
                {
                    var result =
                        MessageBox.Show(
                            "Snoop has noticed windows running in multiple dispatchers.\n\n" +
                            "Would you like to enter multiple dispatcher mode, and have a separate Snoop window for each dispatcher?\n\n" +
                            "Without having a separate Snoop window for each dispatcher, you will not be able to Snoop the windows in the dispatcher threads outside of the main dispatcher. " +
                            "Also, note, that if you bring up additional windows in additional dispatchers (after snooping), you will need to Snoop again in order to launch Snoop windows for those additional dispatchers.",
                            "Enter Multiple Dispatcher Mode",
                            MessageBoxButton.YesNo,
                            MessageBoxImage.Question);

                    if (result == MessageBoxResult.Yes)
                    {
                        useMultipleDispatcherMode = true;
                    }
                }

                if (useMultipleDispatcherMode)
                {
                    SnoopModes.MultipleDispatcherMode = true;

                    var thread = new Thread(DispatchOut);
                    thread.Start(new DispatchOutParameters(settingsData, instanceCreator, rootVisuals));

                    // todo: check if we really created something
                    return(true);
                }
            }

            return(false);
        }