刚刚开始学C#,有些不懂的地方,请诸位指教。下面的代码基本上是msdn的sample,但这里myProcess = Process.Start("myexe.exe");问题一:有时候myexe运行结束的太快,进入while(!myProcess.HasExited)后,但在调用myProcess.WorkingSet之前就结束了,这样会产生错误,请问应该怎么处理呢?问题二:myexe如果产生了run time error,就会有对话框弹出,而主程序也就死了,请问有什么办法可以使主程序继续运行,并可得到myexe异常退出的ExitCode,不弹出对话框呢?在vc里面好象有个SetErrorMode的方法。using System;
using System.Diagnostics;
using System.Threading;namespace Process_Sample
{
class MyProcessClass
{
public static void Main()
{
try
{ Process myProcess;
myProcess = Process.Start("myexe.exe"); while(!myProcess.HasExited)
{
Console.WriteLine(); // Get physical memory usage of the associated process.
Console.WriteLine("Process's physical memory usage: " + myProcess.WorkingSet);
// Get base priority of the associated process.
Console.WriteLine("Base priority of the associated process: " + myProcess.BasePriority);
// Get priority class of the associated process.
Console.WriteLine("Priority class of the associated process: " + myProcess.PriorityClass);
// Get user processor time for this process.
Console.WriteLine("User Processor Time: " + myProcess.UserProcessorTime);
// Get privileged processor time for this process.
Console.WriteLine("Privileged Processor Time: " + myProcess.PrivilegedProcessorTime);
// Get total processor time for this process.
Console.WriteLine("Total Processor Time: " + myProcess.TotalProcessorTime);
// Invoke overloaded ToString function.
Console.WriteLine("Process's Name: " + myProcess.ToString());
Console.WriteLine("-------------------------------------"); if(myProcess.Responding)
{
Console.WriteLine("Status: Responding to user interface");
myProcess.Refresh();
}
else
{
Console.WriteLine("Status: Not Responding");
}
Thread.Sleep(1000); } Console.WriteLine();
Console.WriteLine("Process exit code: {0}", myProcess.ExitCode);
}
catch(Exception e)
{
Console.WriteLine("The following exception was raised: " + e.Message);
}
} }
}
using System.Diagnostics;
using System.Threading;namespace Process_Sample
{
class MyProcessClass
{
public static void Main()
{
try
{ Process myProcess;
myProcess = Process.Start("myexe.exe"); while(!myProcess.HasExited)
{
Console.WriteLine(); // Get physical memory usage of the associated process.
Console.WriteLine("Process's physical memory usage: " + myProcess.WorkingSet);
// Get base priority of the associated process.
Console.WriteLine("Base priority of the associated process: " + myProcess.BasePriority);
// Get priority class of the associated process.
Console.WriteLine("Priority class of the associated process: " + myProcess.PriorityClass);
// Get user processor time for this process.
Console.WriteLine("User Processor Time: " + myProcess.UserProcessorTime);
// Get privileged processor time for this process.
Console.WriteLine("Privileged Processor Time: " + myProcess.PrivilegedProcessorTime);
// Get total processor time for this process.
Console.WriteLine("Total Processor Time: " + myProcess.TotalProcessorTime);
// Invoke overloaded ToString function.
Console.WriteLine("Process's Name: " + myProcess.ToString());
Console.WriteLine("-------------------------------------"); if(myProcess.Responding)
{
Console.WriteLine("Status: Responding to user interface");
myProcess.Refresh();
}
else
{
Console.WriteLine("Status: Not Responding");
}
Thread.Sleep(1000); } Console.WriteLine();
Console.WriteLine("Process exit code: {0}", myProcess.ExitCode);
}
catch(Exception e)
{
Console.WriteLine("The following exception was raised: " + e.Message);
}
} }
}
解决方案 »
- 急!如何读取dat文件
- 2个WINFROM的小问题 判断窗体 或控件内容 是否首次加载 以及 设定快捷键
- System.Web.HttpUtility.UrlEncode为什么不可用
- Directory.GetCurrentDirectory() 方法和 Application.StartupPath 有什么不同
- C# 工厂模式 返回值不匹配,求高手改改
- 【安装程序问题,如何判断Dos命令是否执行成功】
- 熟悉ibatisnet的进来看看,一个关于配置的问题(100分)
- 在C#中如何判断光驱中是否有盘,如何得到光驱的盘符,如何弹出光驱?
- 有关平台调用的问题
- 菜鸟问题,望大家帮忙!
- 时间转换问题
- 用什么方法把大文件从一台机子复制到另一台机子
p.EnableRaisingEvents = false;
p.StartInfo = new System.Diagnostics.ProcessStartInfo("FindText.txt");
p.Start();
p.Close();
这里的myexe.exe是一个有除0错误的C++程序。