主题:[原创]玩Process这样的麻烦也让我碰上了,需要帮助啊!!
sageking2
[专家分:200] 发布于 2008-01-20 16:27:00
我想写个能显示进程信息的win-form,先拖了个listview,一个timer.然后.
private void Form1_Load(object sender, EventArgs e)
{
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
string[] subItem ={process.ProcessName,
process.MachineName ,
process.TotalProcessorTime.ToString(),
string.Format("{0:N}",process.WorkingSet64/1024),
process.Id.ToString()
};
listView1.Items.Add(new ListViewItem(subItem));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
int Pid = int.Parse(item.SubItems[4].Text);
try
{
Process p = Process.GetProcessById(Pid);
item.SubItems[2].Text = p.TotalProcessorTime.ToString();
item.SubItems[3].Text = string.Format("{0:N}K", p.WorkingSet64 / 1024);
}
catch
{
listView1.Items.Remove(item);
}
}
}
结果吓我一跳.弹出Win32Exception was unhandled.
string[] subItem ={process.ProcessName,
process.MachineName ,
process.TotalProcessorTime.ToString(),
string.Format("{0:N}",process.WorkingSet64/1024),
process.Id.ToString()
};
变黄了. 初来混C#,没见过什么市面,还忘楼下的教育一下.
回复列表 (共6个回复)
沙发
jzyray [专家分:20610] 发布于 2008-01-20 21:01:00
看错误提示是什么
板凳
sageking2 [专家分:200] 发布于 2008-01-21 08:46:00
错误提示只有这个
弹出Win32Exception was unhandled.
string[] subItem ={process.ProcessName,
process.MachineName ,
process.TotalProcessorTime.ToString(),
string.Format("{0:N}",process.WorkingSet64/1024),
process.Id.ToString()
};
变黄了.
后来我点了下Detail去剪贴版里看到这么个东西.
System.ComponentModel.Win32Exception was unhandled
Message="拒绝访问。"
Source="System"
ErrorCode=-2147467259
NativeErrorCode=5
StackTrace:
at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.GetProcessTimes()
at System.Diagnostics.Process.get_TotalProcessorTime()
at 战备进程管理器.Form1.Form1_Load(Object sender, EventArgs e) in C:\Documents and Settings\主席夸我长的帅\My Documents\Visual Studio 2005\Projects\SAProcess\战备进程管理器\Form1.cs:line 24
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at 战备进程管理器.Program.Main() in C:\Documents and Settings\主席夸我长的帅\My Documents\Visual Studio 2005\Projects\SAProcess\战备进程管理器\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
3 楼
jzyray [专家分:20610] 发布于 2008-01-21 18:12:00
process.TotalProcessorTime属性获取的时候出了问题
你debug一下,看看是哪个进程
4 楼
sageking2 [专家分:200] 发布于 2008-01-22 15:31:00
string[] subItem ={process.ProcessName,
process.MachineName ,
process.TotalProcessorTime.ToString(),
string.Format("{0:N}",process.WorkingSet64/1024),
process.Id.ToString()
};
我在前面用MessageBox.Show(process.ProcessName+process.MachineName +...);发现除
process.MachineName乱码其他的都没问题.
应该是process.MachineName有问题.我的目的是要它显示进程的用户名.
eg:在我的机器上有
SYSTEM
主席夸我长的帅
LOCAL SERVERS
这几个用户名有中文的.我只知道一般的IO可以用(process.MachineName ,System.Text.Encoding.Default)来搞顶.这里是无能为力了.
事情更奇怪的是:process.ProcessName的中文进程名能正确显示.谢谢
jzyray反复关注, 好人作到底啊!
5 楼
jzyray [专家分:20610] 发布于 2008-01-22 19:44:00
Encoding的GetString/GetBytes等函数可以用于编码转换
IO的构造函数其实也是通过这种方式进行的
6 楼
sageking2 [专家分:200] 发布于 2008-01-23 10:39:00
终于找到错误原因了(放弃边缘上的一束闪光):问题在于读取最后一个进程
System Idel Process(系统空闲进程)时无法访问.用try catch可以绕过它,不过每次都先弹出个"拒绝访问"在弹出程序界面(或者不去处理catch事件欺骗群众^-^).
1.如果有更好方法别忘了告诉我.
2.GetString/GetBytes只能把单个汉字转来转去,多了就比较麻烦.奇怪process.ProcessName里的汉字可以正常显示, process.MachineName里的就不行.C#真它吗有意思.
我来回复