zoukankan
html css js c++ java
一个简单的进程察看器
using
System;
using
System.Diagnostics;
namespace
ProcessManager
{
/**/
///
<summary>
///
Summary description for Class1.
///
</summary>
class
MainEntry
{
/**/
///
<summary>
///
The main entry point for the application.
///
</summary>
[STAThread]
static
void
Main(
string
[] args)
{
if
(args.Length
<
1
)ListProcesses();
else
if
(args[
0
].Length
>
0
)ListProcess(args[
0
]);
}
private
static
void
ListProcesses()
{
string
output
=
""
;
int
limit
=
36
;
Console.WriteLine(
"
PID
"
.PadRight(
6
)
+
"
Name
"
.PadRight(
16
)
+
"
Memory
"
.PadLeft(
10
)
+
"
VM size
"
.PadLeft(
10
)
+
"
Threads count
"
.PadLeft(
20
));
Console.WriteLine(
"
-
"
.PadRight(limit
+
34
,
'
-
'
));
foreach
(Process proc
in
Process.GetProcesses())
{
string
pid
=
proc.Id.ToString();
string
pname
=
proc.ProcessName;
string
pcount
=
proc.Threads.Count.ToString();
string
pmemsize
=
(proc.PeakWorkingSet
/
1024
).ToString()
+
"
K
"
;
string
pvmsize
=
(proc.PeakPagedMemorySize
/
1024
).ToString()
+
"
K
"
;
output
=
String.Format(
"
{0}{1}{2}{3}{4}
"
,pid.PadRight(
6
),pname.PadRight(
16
),pmemsize.PadLeft(
10
),pvmsize.PadLeft(
10
),pcount.PadLeft(
10
));
Console.WriteLine(output);
}
}
private
static
void
ListProcess(
string
processid)
{
int
pid
=
-
1
;
try
{
pid
=
Convert.ToInt32(processid);
}
catch
{
}
if
(
-
1
==
pid)
{
Console.WriteLine(
"
Invalid processID!
"
);
return
;
}
string
output
=
""
;
Process proc
=
null
;
try
{
proc
=
Process.GetProcessById(pid);
}
catch
{
}
if
(
null
==
proc)
{
Console.WriteLine(
"
Invalid processID!
"
);
return
;
}
Console.WriteLine(
"
PID:
"
+
proc.Id.ToString());
Console.WriteLine(
"
-
"
.PadRight(
50
,
'
-
'
));
//
Console.WriteLine("Main Module");
//
Console.WriteLine("-".PadRight(50,'-'));
Console.WriteLine(
"
Module Name
"
.PadRight(
50
)
+
"
Memory Size
"
);
WriteModuleInformation(proc.MainModule);
Console.WriteLine(
"
-
"
.PadRight(
50
,
'
-
'
));
//
Console.WriteLine("All Modules");
//
Console.WriteLine("-".PadRight(50,'-'));
Console.WriteLine(
"
Module Name
"
.PadRight(
50
)
+
"
Memory Size
"
);
foreach
(ProcessModule pm
in
proc.Modules)WriteModuleInformation(pm);
Console.WriteLine(
"
-
"
.PadRight(
50
,
'
-
'
));
Console.WriteLine(
"
Thread List:
"
);
Console.WriteLine(
"
-
"
.PadRight(
50
,
'
-
'
));
Console.WriteLine(
"
ID
"
.PadRight(
6
)
+
"
Thread State
"
.PadRight(
20
)
+
"
Total Processor Time
"
);
Console.WriteLine(
"
-
"
.PadRight(
50
,
'
-
'
));
foreach
(ProcessThread pt
in
proc.Threads)
{
output
=
String.Format(
"
{0}{1}{2}
"
,pt.Id.ToString().PadRight(
6
),pt.ThreadState.ToString().PadRight(
20
),pt.TotalProcessorTime);
Console.WriteLine(output);
}
}
private
static
void
WriteModuleInformation(ProcessModule pm)
{
Console.WriteLine(String.Format(
"
{0}{1}
"
,pm.ModuleName.PadRight(
50
),(pm.ModuleMemorySize
/
1024
).ToString().PadLeft(
10
)
+
"
K
"
));
}
}
}
查看全文
相关阅读:
代码中一些常见的小片段
Surface Pro 3 扩展坞体验
Microsoft SQL Server 存储过程举例
Google Guava学习笔记——基础工具类针对Object类的使用
Google Guava学习笔记——基础工具类Preconditions类的使用
Google Guava学习笔记——基础工具类String处理类的使用
Google Guava学习笔记——基础工具类Splitter的使用
Google Guava学习笔记——基础工具类Joiner的使用
Google Guava学习笔记——简介
好书推荐——《Soft Skill》
原文地址:https://www.cnblogs.com/juqiang/p/51322.html
最新文章
DFX 安全测试-- 告诉你什么是XSS、sql注入?POST和GET的区别....
用最简单话概括SSH三框架
Tomcat+Nginx+Lvs部署方案与性能调优
建议测试人员提单格式,统一、高效一步到位!
如果你是项目经理看看你做到了吗?
职业生涯规划-技术还是管理
工作经验总结:百万数据引发的性能瓶颈问题
CRM项目经验总结-从DAO层到链接数据池
Oracle窗口函数显示想要的行数
《Soft Skill》一书中的好句子
热门文章
使用WatchService监控指定目录内的文件的改动
使用 NIO.2 遍历目录下所有的Java文件
使用try-with-resources注意的问题
OS X 使用技巧——在Finder窗口标题栏上显示路径
OS X 使用技巧——不用鼠标就能打开应用程序
《你的时间有限,不要为别人而活》句子摘选
OS X 使用技巧——访问所有的键盘功能
OS X 使用技巧——轻松地调整窗口大小
BCP command usage in SQL Server
SQLServer BCP 命令的使用
Copyright © 2011-2022 走看看