C# 启动另一个应用程序,但不启动新流程
所以我发现C# 启动另一个应用程序,但不启动新流程,c#,.net,wpf,C#,.net,Wpf,所以我发现 System.Diagnostics.Process.Start() 但,然后我想关闭启动它的应用程序(我需要启动程序连接到服务器,启动游戏并关闭自己),这样它就不工作了。 我的应用程序: 命名进程,以便有一个句柄来关闭它。并非所有进程都能正常关闭 try { Process myProcess; myProcess = Process.Start("Notepad.exe"); // Display physic
System.Diagnostics.Process.Start()
但,然后我想关闭启动它的应用程序(我需要启动程序连接到服务器,启动游戏并关闭自己),这样它就不工作了。
我的应用程序:
命名进程,以便有一个句柄来关闭它。并非所有进程都能正常关闭
try
{
Process myProcess;
myProcess = Process.Start("Notepad.exe");
// Display physical memory usage 5 times at intervals of 2 seconds.
for (int i = 0;i < 5; i++)
{
if (!myProcess.HasExited)
{
// Discard cached information about the process.
myProcess.Refresh();
// Print working set to console.
Console.WriteLine("Physical Memory Usage: "
+ myProcess.WorkingSet.ToString());
// Wait 2 seconds.
Thread.Sleep(2000);
}
else {
break;
}
}
// Close process by sending a close message to its main window.
myProcess.CloseMainWindow();
// Free resources associated with process.
myProcess.Close();
}
catch(Exception e)
{
Console.WriteLine("The following exception was raised: ");
Console.WriteLine(e.Message);
}
试试看
{
过程我的过程;
myProcess=Process.Start(“Notepad.exe”);
//以2秒的间隔显示物理内存使用情况5次。
对于(int i=0;i<5;i++)
{
如果(!myProcess.hasExit)
{
//丢弃有关进程的缓存信息。
myProcess.Refresh();
//将工作集打印到控制台。
Console.WriteLine(“物理内存使用:
+myProcess.WorkingSet.ToString());
//等2秒钟。
《睡眠》(2000年);
}
否则{
打破
}
}
//通过向进程的主窗口发送关闭消息来关闭进程。
myProcess.CloseMainWindow();
//与进程关联的可用资源。
myProcess.Close();
}
捕获(例外e)
{
WriteLine(“引发了以下异常:”);
控制台写入线(e.Message);
}
使用进程。启动(ProcessStartInfo)
重载,并将UseShellExecute
属性设置为true
另外,什么“不起作用”?发生了什么,你想要什么?也许“不工作”意味着代码没有编译:)这段代码没有意义:它是一个类定义,但没有方法(或构造函数)来保存代码。这不应该编译!问题和提供的代码都没有意义。。。很难对此做出合理的回答。嗯,我认为OP想要关闭启动流程的应用程序,而不是流程launched@Steve好的,我想我同意你的解释。但目前还不清楚。我将把它搁置一天。很抱歉被误解了。这就是我想要的。
try
{
Process myProcess;
myProcess = Process.Start("Notepad.exe");
// Display physical memory usage 5 times at intervals of 2 seconds.
for (int i = 0;i < 5; i++)
{
if (!myProcess.HasExited)
{
// Discard cached information about the process.
myProcess.Refresh();
// Print working set to console.
Console.WriteLine("Physical Memory Usage: "
+ myProcess.WorkingSet.ToString());
// Wait 2 seconds.
Thread.Sleep(2000);
}
else {
break;
}
}
// Close process by sending a close message to its main window.
myProcess.CloseMainWindow();
// Free resources associated with process.
myProcess.Close();
}
catch(Exception e)
{
Console.WriteLine("The following exception was raised: ");
Console.WriteLine(e.Message);
}