Dewagg
Dewagg

Reputation: 145

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. I want to check if gta_sa.exe process was closed, then close my app.

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)

My code is closing samp.exe, but I want to close gta_sa.exe.

Upvotes: 0

Views: 178

Answers (2)

Eric J.
Eric J.

Reputation: 150108

It work's, but when launches samp.exe, name changes to gta_sa.exe.

It sounds like samp.exe is a launcher for 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. You can then create a Process instance from the running process and add an event handler for Exited.

P.S my code closing samp.exe, but I wan't to close gta_sa.exe..

No, it is not. Your code is being alerted when samp.exe closes on its own (or for some other reason). If samp.exe is indeed a launcher, its normal behavior would be to close after it starts gta_sa.exe.

If you want to close gta_sa.exe, you can do that using Process.Kill().

You can set an event handler for gta_sa.exe closing like this

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. Normally there will be only one item in processes, the one process that was launched by samp.exe.

Upvotes: 3

msmolcic
msmolcic

Reputation: 6557

When your samp.exe process exits try to get "gta_sa.exe" process and terminate it:

void p_Exited(object sender, EventArgs e)
{
    var processes = Process.GetProcessesByName("gta_sa.exe");
    foreach (var process in processes)
        process.Kill();
}

Upvotes: 0

Related Questions