[英]C# Is it possible to close that process?
I have this code: 我有以下代码:
private void button1_Click(object sender, EventArgs e)
{
Process p = new Process();
string path = AppDomain.CurrentDomain.BaseDirectory + "samp.exe";
string arguments = "arguments...";
p.Exited += new EventHandler(p_Exited);
p.StartInfo.FileName = path;
p.EnableRaisingEvents = true;
p.Start();
}
And 和
void p_Exited(object sender, EventArgs e)
{
MessageBox.Show("it works");
}
It works, but when I launch samp.exe
, the name changes to gta_sa.exe
. 它可以工作,但是当我启动
samp.exe
,名称更改为gta_sa.exe
。 I want to check if gta_sa.exe
process was closed, then close my app. 我想检查
gta_sa.exe
进程是否关闭,然后关闭我的应用程序。
SHORTLY: I want to make a button. 简短地说:我想做个按钮。 When I click on, it launches
samp.exe
process, but samp.exe
renames to gta_sa.exe
, so I need to check if gta_sa.exe
process was closed, close my app (Test.exe) 当我单击时,它将启动
samp.exe
进程,但是samp.exe
重命名为gta_sa.exe
,因此我需要检查gta_sa.exe
进程是否已关闭,关闭我的应用程序(Test.exe)
My code is closing samp.exe
, but I want to close gta_sa.exe
. 我的代码正在关闭
samp.exe
,但是我想关闭gta_sa.exe
。
It work's, but when launches samp.exe, name changes to gta_sa.exe.
可以,但是启动samp.exe时,名称更改为gta_sa.exe。
It sounds like samp.exe is a launcher for gta_sa.exe. 听起来samp.exe是gta_sa.exe的启动器。 That is, the first process starts the second process.
即,第一个过程开始第二个过程。 If samp.exe does not wait for gta_sa.exe to exit, you will have to find the running instance of gta_sa.exe.
如果samp.exe不等待gta_sa.exe退出,则必须找到正在运行的gta_sa.exe实例。 You can then create a Process instance from the running process and add an event handler for Exited.
然后,您可以从正在运行的进程中创建Process实例,并为Exited添加事件处理程序。
PS my code closing samp.exe, but I wan't to close gta_sa.exe..
PS我的代码关闭samp.exe,但我不想关闭gta_sa.exe。
No, it is not. 不它不是。 Your code is being alerted when samp.exe closes on its own (or for some other reason).
当samp.exe自行关闭(或出于某些其他原因)时,您的代码将收到警报。 If samp.exe is indeed a launcher, its normal behavior would be to close after it starts gta_sa.exe.
如果samp.exe确实是启动器,则其正常行为是启动gta_sa.exe后关闭。
If you want to close gta_sa.exe, you can do that using Process.Kill() . 如果要关闭gta_sa.exe,可以使用Process.Kill()来完成 。
You can set an event handler for gta_sa.exe closing like this 您可以像这样关闭gta_sa.exe设置事件处理程序
var processes = Process.GetProcessesByName("gta_sa.exe");
foreach (var p in processes)
{
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);
}
Make sure you wait to run this code until after gta_sa.exe has been started. 确保等到gta_sa.exe启动后再运行此代码。 Normally there will be only one item in processes, the one process that was launched by samp.exe.
通常,进程中将只有一项,即samp.exe启动的一项。
When your samp.exe process exits try to get "gta_sa.exe" process and terminate it: 当您的samp.exe进程退出时,尝试获取“ gta_sa.exe”进程并终止它:
void p_Exited(object sender, EventArgs e)
{
var processes = Process.GetProcessesByName("gta_sa.exe");
foreach (var process in processes)
process.Kill();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.