Call Windows Task Manager with the Performance tab selected

I am currently invoking the Windows Task Manager using the click event in WPF. The event simply executes "Process.Start (" taskmgr ").

My question is, is there a way to choose which tab inside the task manager is selected when the process starts / displays? I want the Performance tab to be selected automatically whenever a click event is fired.

Thanks for the help.

+6
source share
3 answers

To go to the post of Philip Schmid, I slightly weighed the demo version:

Run it as a console application. You need to add links to UIAutomationClient and UIAutomationTypes .

One of the possible improvements that you (or I, if you wish) can make is to hide the window initially, only showing it after the correct tab has been selected. I'm not sure if this will work, but I'm not sure if AutomationElement.FromHandle can find a hidden window.

Edit: At least on my computer (Windows 7, 32 bit, .Net framework 4.0) the following code initially creates a hidden task manager and shows it after the correct tab has been selected. I do not show the window after selecting the performance tab, so perhaps one of the automation lines does this as a side effect.

 using System; using System.Diagnostics; using System.Windows.Automation; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { // Kill existing instances foreach (Process pOld in Process.GetProcessesByName("taskmgr")) { pOld.Kill(); } // Create a new instance Process p = new Process(); p.StartInfo.FileName = "taskmgr"; p.StartInfo.CreateNoWindow = true; p.Start(); Console.WriteLine("Waiting for handle..."); while (p.MainWindowHandle == IntPtr.Zero) ; AutomationElement aeDesktop = AutomationElement.RootElement; AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle); Console.WriteLine("Got handle"); // Get the tabs control AutomationElement aeTabs = aeForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tab)); // Get a collection of tab pages AutomationElementCollection aeTabItems = aeTabs.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem)); // Set focus to the performance tab AutomationElement aePerformanceTab = aeTabItems[3]; aePerformanceTab.SetFocus(); } } } 

Why am I destroying previous instances of the task manager? When the instance is already open, the secondary instances open but close immediately. My code does not check this, so the code that finds the window handle will be frozen.

+5
source

While taskmgr.exe has no command line arguments for specifying the selected tab, you can use Windows UI Automation to go to the "performance tab."

+2
source

Unfortunately, taskmgr.exe does not support any command line argument.

At startup, it always activates the tab that was active the last time it was closed.

+1
source

Source: https://habr.com/ru/post/894573/


All Articles