Invoking windows task manager with 'performance' tab selected
I am currently invoking the windows task manager using a click event in WPF. The event simply executes 'Process.Start("taskmgr").
My question is, is there a way to choose which tab inside task manager is selected when the process starts / is displayed? I am looking to have the 'performance' tab selected automatically whenever the click event is raise开发者_开发知识库d.
Thanks for the help.
To expand on Philipp Schmid's post, I've whipped up a little demo:
Run it as a console application. You need to add references to UIAutomationClient
and UIAutomationTypes
.
One possible improvement you (or I, if you desire) can make is to hide the window initially, only showing it after the correct tab has been selected. I'm not sure if that would work, however, as I'm not sure that AutomationElement.FromHandle
would be able to 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 don't explicitly show the window after selecting the performance tab, so probably one of the automation lines does 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 do I destroy previous instances of Task Manager? When an instance is already open, secondary instances will open but immediately close. My code doesn't check for this, so the code that finds the window handle will freeze.
While taskmgr.exe doesn't have any command line arguments to specify the selected tab, you can use Windows UI Automation to 'navigate' to the performance tab.
Unfortunately, taskmgr.exe
does not support any command line argument.
When run, it will always activate the tab that was active on last close.
Starting with Windows 10 build 18305, you can now set a preferred tab to have Task Manager open to by default.
To update:
- Click on the start menu and in the search box type 'Windows Update'
- Chose 'Windows Update Settings'
- In the left panel click 'Preview Builds'
- Click on the 'Check' now.
- Download the new build.
After update, change dword value of StartUpTab
in Win registry key:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\TaskManager
0 – Processes tab
1 – Performance tab
2 – App history tab
3 – Startup tab
4 – Users tab
5 – Details tab
6 – Services tab
Win CMD:
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\TaskManager /v "startup" /t REG_DWORD /d "1"
This (experimental) feature is only available to some Windows Insiders.
No other tabs except "Start-up" are supported for older builds of Win 10:
taskmgr /4 /startup
To reset:
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\TaskManager /v "Preferences" /f
To confirm modified key:
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "HKCU\Software\Microsoft\Windows\CurrentVersion\TaskManager" /f & regedit
Tested in Win 10 CMD
精彩评论