[C#]How To End A Process

Joined
Dec 25, 2008
Messages
537
Reaction score
1
I pretty much suck with C#, but how do I end a process?

This is an example from my code:

Code:
        private void button2_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();
            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Kill();

            
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
        }

But when I do that, it just says that the process cannot be found.
Help please?
 
Code:
        private void button2_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();
            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Kill();
 
 
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
[B]            break;[/B]
        }
 
Code:
        private void button2_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();
            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Kill();
 
 
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
[B]            break;[/B]
        }

Actually, that gives me an error.
on the break.

"No enclosing loop out of which to break or continue"
 
Lol, u need to make a start button etc
Code:
Process startServer = new Process();

        private void startbutton_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = textBox1.Text;
            startServer.Start();
        }

        private void closebutton_Click(object sender, EventArgs e)
        {
            startServer.Kill();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
        }
 
Lol, u need to make a start button etc
Code:
Process startServer = new Process();

        private void startbutton_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = textBox1.Text;
            startServer.Start();
        }

        private void closebutton_Click(object sender, EventArgs e)
        {
            startServer.Kill();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
        }

I already did that.
@bounty-hunter: I'll try that.
EDIT:
When i change it to "startserver", i get this:
Code:
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidOperationException: No process is associated with this object.
   at System.Diagnostics.Process.EnsureState(State state)
   at System.Diagnostics.Process.EnsureState(State state)
   at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
   at System.Diagnostics.Process.Kill()
   at ServerRestart.Form1.button2_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
ServerRestart
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/Tony/Desktop/SoyMSdump/main%20stuff/ServerRestart.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
 
I already did that.
@bounty-hunter: I'll try that.
EDIT:
When i change it to "startserver", i get this:
Code:
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidOperationException: No process is associated with this object.
   at System.Diagnostics.Process.EnsureState(State state)
   at System.Diagnostics.Process.EnsureState(State state)
   at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
   at System.Diagnostics.Process.Kill()
   at ServerRestart.Form1.button2_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
ServerRestart
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/Tony/Desktop/SoyMSdump/main%20stuff/ServerRestart.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

r u sure u already tried it? cuz it sure works for me... -.-
 
Well, this is my whole form1:

Code:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ServerRestart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Step 1.  Name your bat files.  In the provided textboxes.  Input the name of the bat files.  (e.g If your first bat to be launched is world.bat, put world.bat");
            MessageBox.Show("OPTIONAL: State the time you want in between bat files launched.");
            MessageBox.Show("Step 2. Click the 'Start Server' button. ");
            MessageBox.Show("If at anytime you want to shut down, click the 'Close Server' button.  To restart, click the 'Restart Server' button.");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();

            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Start();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instances of server are running. (Java)\r\n");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();
            startServer.StartInfo.FileName = "serverstart";
            startServer.Kill();


            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();

            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Kill();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
            startServer.Start();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instances of server are running. (Java)\r\n");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();

            startServer.StartInfo.FileName = "Maplestoryserver.exe";
            startServer.Start();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();

            startServer.StartInfo.FileName = "Maplestoryserver.exe";
            startServer.Kill();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Process startServer = new Process();

            startServer.StartInfo.FileName = "Maplestoryserver.exe";
            startServer.Kill();
            startServer.Start();
        }

        private void setAutoRestartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 restarttime = new Form2();
            restarttime.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Process startServer = new Process();

            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Kill();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
            startServer.Start();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instances of server are running. (Java)\r\n");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            timer1.Interval = int.Parse(textBox1.Text);
            timer1.Start();

        }

        private void creditsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form2 credits = new Form2();
            credits.Show();
        }

        private void visitSoyDEVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("http://soy.no-ip.biz/");
        }

        private void calculatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("calc.exe");
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
    }
}
 
Yep, you didn't do what I told u -.-.
You should have this: -.-
Code:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ServerRestart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

	    Process startServer = new Process();

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Step 1.  Name your bat files.  In the provided textboxes.  Input the name of the bat files.  (e.g If your first bat to be             launched is world.bat, put world.bat");
            MessageBox.Show("OPTIONAL: State the time you want in between bat files launched.");
            MessageBox.Show("Step 2. Click the 'Start Server' button. ");
            MessageBox.Show("If at anytime you want to shut down, click the 'Close Server' button.  To restart, click the 'Restart Server' button.");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Start();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instances of server are running. (Java)\r\n");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "serverstart";
            startServer.Kill();


            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Kill();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
            startServer.Start();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instances of server are running. (Java)\r\n");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "Maplestoryserver.exe";
            startServer.Start();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "Maplestoryserver.exe";
            startServer.Kill();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            startServer.StartInfo.FileName = "Maplestoryserver.exe";
            startServer.Kill();
            startServer.Start();
        }

        private void setAutoRestartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 restarttime = new Form2();
            restarttime.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            startServer.Kill();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instaces of server have been stopped. (Java)\r\n");
	    startServer.StartInfo.FileName = "serverstart.bat";
            startServer.Start();
            richTextBox1.Text = richTextBox1.Text + DateTime.Today.ToString("MM/dd/yyyy") + (" - All instances of server are running. (Java)\r\n");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            timer1.Interval = int.Parse(textBox1.Text);
            timer1.Start();

        }

        private void creditsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form2 credits = new Form2();
            credits.Show();
        }

        private void visitSoyDEVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("http://soy.no-ip.biz/");
        }

        private void calculatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("calc.exe");
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
    }
}

Btw, if you need help and we post something try it, and don't say u did.
We are trying to help you, but you need to try it out.
After that I might not help you as much.
 
Ugh, here...

Code:
private void CloseBtn_Click(object sender, EventArgs e)
        {
            foreach (Process XZeenonProcess in Process.GetProcesses())
            {
                if (XZeenonProcess.ProcessName.StartsWith(NOTEPAD))
                {
                    XZeenonProcess.Kill();
                }
            }
        }

where it says NOTEPAD change that to w/e process u want to kill but do not add .exe at the end. Just the name.

Credits to me, oviously lol
 
Back