Results 1 to 10 of 10

Thread: [RESOLVED] Kill spesific proccess useing C#

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] Kill spesific proccess useing C#

    Hi all,

    I have searched up and down and i can only find this VB example on how to do this. needless to say i am having trouble converting it over to c#

    http://www.vbforums.com/showpost.php...03&postcount=2

    For Each proc As Process In Process.GetProcessesByName("process name here");

    proc.CloseMainWindow() //ask the process to exit.;

    proc.WaitForExit(10000) //wait up to 10 seconds.



    If Not proc.HasExited Then
    {
    proc.Kill() //force the process to exit.

    }

    Next proc


    I understand the idea but im having trouble with the syntax

    do i need to import something to for it to recoginize proccess?
    Last edited by Crash893; Jun 4th, 2007 at 12:44 PM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Kill spesific proccess useing C#

    Here you go.
    C# Code:
    1. foreach(System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName("process name here"))
    2.             {
    3.                 proc.CloseMainWindow();
    4.                 proc.WaitForExit(10000);
    5.  
    6.                 if (!proc.HasExited)
    7.                 {
    8.                     proc.Kill();
    9.                 }
    10.             }

    You could import System.Diagnostics if you'd like.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Kill spesific proccess useing C#

    hey im having some trouble getting it working

    i can see the proccess is WINWORD.EXE in taskman but when i put that in to the textbox1.text it doesnt enter the for loop and proc = null

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Kill spesific proccess useing C#

    Yeah you have to exclude the '.exe' part, just type WINWORD
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Kill spesific proccess useing C#

    Ha

    I just figured that out on my own like 7 seconds ago and came online to let you know i got it

    could have saved my self the day or so of tinkering

    Thanks for your help
    rate = rate +1

  6. #6

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [RESOLVED] Kill spesific proccess useing C#

    c# Code:
    1. foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName(textBox1.Text))
    2. {
    3.     proc.CloseMainWindow();
    4.     proc.WaitForExit(10000);
    5.     if (!proc.HasExited)
    6.     {
    7.         proc.Kill();
    8.     }
    9.     proc.Dispose();
    10. }

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] Kill spesific proccess useing C#

    Good news
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [RESOLVED] Kill spesific proccess useing C#

    Im having a small problem

    should i post it here or open a new thread?

    Ill post it here till told to do otherwise


    I am trying to kill the process for a particular session on a Terminal server.

    when i run this program it kills ALL the process regardless of session

    how can i narrow this down kill the process only on the specific session that it is run on.


    For example it killed everything in RED but i only wanted to kill my wordctrl in my session ( in blue)

    Last edited by Crash893; Jun 3rd, 2007 at 08:14 PM.

  9. #9

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Kill spesific proccess useing C#

    I think im on to something

    i found that i can do a proc.session ID that will give me the id of the process
    Does anyone know where i can find the current sessionID

    i would think it would be under
    System.Environment.???? something but i cnat find it.

  10. #10

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Kill spesific proccess useing C#

    System.Diagnostics.Process.GetCurrentProcess().SessionId;



    c# Code:
    1. int ThisSession = System.Diagnostics.Process.GetCurrentProcess().SessionId;
    2.                 int thisPID = System.Diagnostics.Process.GetCurrentProcess().Id;
    3.                 foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName(application))
    4.                 {
    5.                     if (proc.SessionId == ThisSession)
    6.                     {
    7.                         proc.CloseMainWindow();
    8.                         proc.WaitForExit(Wait);
    9.                         if (!proc.HasExited)
    10.                         {
    11.                             proc.Kill();
    12.                         }
    13.                         proc.Dispose();
    14.                     }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width