Use Javascript to implement Windows Task Manager

Source: Internet
Author: User
In Windows, the Script Host (WSH) function has been provided since the 98 system. WSH can load and run JS and VBS scripts and call the COM components of the system, with the support of COM components, scripts can easily implement very powerful functions .... SyntaxHighlighter.

On Windows, WSH has provided the Script Host (WSH for short) function since the 98 system. WSH can load and run JS and VBS scripts, it also supports calling system COM components. With the support of COM components, scripts can easily implement very powerful functions.
Many people may think of web pages when talking about JS. In fact, JS can also take the initiative to do things that can be done in other languages, or even dominate the browser in turn, rather than being dominated by the browser.
This article will introduce a Windows Task Manager that is built on the WSH platform and is truly available through JS.
I. Code and comments
 
/*
JS Task Manager
By: X! Ao_f
Mail: Xiao_f.mail@163.com
QQ: 120000512
*/
// Create an object Shell object that provides basic functions such as a pop-up prompt box, running processes, and operating the registry.
Var shell = WScript. CreateObject ("WScript. Shell ");
// Create a WMI Object and implement system management through WMI, which includes the Process Management Section.
Var wmi = WScript. CreateObject ("WbemScripting. SWbemLocator"). ConnectServer (".", "root \ cimv2 ");
// Create an IE window for data output
Var browser = WScript. CreateObject ("InternetExplorer. Application ");
// Microsoft's public dialog box component provides the ability to open files, select fonts, and select colors
Var commonDialog = WScript. CreateObject ("MSComDlg. CommonDialog ");
Var window;

Function foreach (object, fn ){
Var I = 0;
For (var e = new Enumerator (object );! E. atEnd (); e. moveNext ()){
Fn (I ++, e. item ());
}
}

// Initialization window
~ Function (){
Browser. navigate ("about: blank ");
Browser. visible = false
Browser.doc ument. write ('\
\
\
\
\
\
');
// Wait until the browser is loaded
While (browser. Busy) WScript. Sleep (100 );
// Set the browser appearance
Browser.doc ument. title = "Process Manager"
Browser. toolBar = false;
Browser. statusBar = false;
Browser. fullScreen = true;
Var w = browser. width;
Var h = browser. height;
Browser. fullScreen = false;
Browser. width = w;
Browser. height = h;
Browser. left = 0;
Browser. top = 0;
Window = browser.doc ument. parentWindow;
// Create button and bind the event
Var button = browser.doc ument. createElement ("button ");
Button. innerHTML = "new process ..";
Button. onmousedown = open;
Browser.doc ument. body. appendChild (button );

Var button = browser.doc ument. createElement ("button ");
Button. innerHTML = "Refresh list ";
Button. onmousedown = refresh;
Browser.doc ument. body. appendChild (button );

Var divList = browser.doc ument. createElement ("div ");
DivList. id = "divList ";
Browser.doc ument. body. appendChild (divList );

// Callback function for the process to end
Browser.doc ument. _ kill _ = function (pid ){
Var process = wmi. ExecQuery ("Select * From Win32_Process Where ProcessID = '" + pid + "'")
Foreach (process, function (I, o ){
O. terminate ();
});
};
Browser. visible = true
}()

// Update the list
Function update (msg ){
Browser.doc ument. body. all. divList. innerHTML = msg;
}

// Create a process
Function open (){
// Because the first pop-up dialog box will be blocked by the browser window, hiding the browser can solve this problem.
If (! CommonDialog. Filter ){
Browser. visible = false
Browser. visible = true
}
// Set the filter rules in the dialog box
CommonDialog. Filter = "all types (*.*)";
CommonDialog. DialogTitle = "Process Manager-Select File ";
CommonDialog. MaxFileSize = 260;
CommonDialog. CancelError = false;
// Open the dialog box by means of latency to ensure that the dialog box is displayed at the frontend.
Window. setTimeout (function (){
CommonDialog. ShowOpen ();
Var path = commonDialog. Filename;
// If the Path is not empty, it can be regarded as a confirmation button and executed by shell.
If (path ){
Shell. run ('"' + path + '"');
CommonDialog. Filename = '';
ListProcess ();
}
}, 10 );
}

// Refresh
Function refresh (){
ListProcess ();
}

// Main functions for processing the Process List
Function listProcess (){
// WMI is used to query basic information about all processes. You can use WMI to query related information or run wmic-? In cmd -? View help information.
Var process = wmi. ExecQuery ("Select * from Win32_Process ");
Var p1 = {};
Var p2 = {};
// The CPU usage is calculated below, reference: http://www.techish.net/2009/03/get-process-cpu-usage-using-wmi/
Var data1 = wmi. ExecQuery ("select * from Win32_PerfRawData_PerfProc_Process ");
Foreach (data1, function (I, object ){
P1 [object. IDProcess] = object;
});
WScript. sleep (1000)
Var data2 = wmi. ExecQuery ("select * from Win32_PerfRawData_PerfProc_Process ");
Foreach (data2, function (I, object ){
P2 [object. IDProcess] =
(P1 [object. IDProcess]. PercentProcessorTime-object. PercentProcessorTime )/
(P1 [object. IDProcess]. TimeStamp_Sys100NS-object. TimeStamp_Sys100NS) * 100;
});
// Generate and update the table
Var table = [];
Var memSum = 0;
Var cpuSum = 0;
Table. push (''+ ['Sequence number', 'pid ', 'name', 'operation', 'cpu usage', 'memory usage ', 'path']. join ('') +'');
Foreach (process, function (I, object ){
Var tr = [];
Var td = [];
Td. push (I );
Td. push (object. processid );
Td. push (object. name );
Td. push ('terminal ');
Td. push ((~~ (P2 [object. ProcessID] * 100)/100 );
Td. push ((~~ (Object. WorkingSetSize/1024/1024) * 100)/100 + 'mb ');
Td.push(object.exe cutablepath | '-') + '');
MemSum + = (object. WorkingSetSize/1024/1024 );
If (object. processid! = 0 ){
CpuSum + = p2 [object. ProcessID];
}
Tr. push (''+ Td. join ('') +'');
Table. push (tr );
});
Table. push ('Memory usage: '+ ((~~ (MemSum * 100)/100) + 'mb' + ', CPU usage:' + ((~~ (CpuSum * 100)/100) + '%');
Table ='

'+ Table. join ('') +'
'
Update (table );
}

// There must be a loop here, because the WScript process is separate from the browser process. If no loop is added, the WScript will end when the program is executed here.
Try {
While (! Browser. Closed) {try {refresh () ;}catch (e) {}; WScript. Sleep (1000 )};
} Catch (e ){}

 
Running effect:




 

Ii. Source Code download

/Files/xiao-f/Process.zip http://up.2cto.com/2012/0327/20120327093642385.zip
 


 

Running method: the premise is that the windows operating system, if. the js open mode has no special settings. You can run it by double-clicking it. If you have joined the IDE, You need to select the open mode as "Microsoft Windows Based Script Host ";

 

From X! Ao_f
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.