locked
.NET 1.1 process.kill? RRS feed

  • Question

  • Hi.

    in .NET 1.1, I can create a process:


    Process someProcess = new Process();

    someProcess.StartInfo.FileName = this.someTextBox.Text;

    someProcess.Start();


     

    However, if i wish to kill that process, I have tried using:


    someProcess.Kill();

    //AND:

    someProcess.Close();


     

     

    however both of the processes to close/kill results in an exception:

    "No process is associated with this object"

     

    Now that is quite understandable BUT how can I make an object of a proper "program/application" which the process can be killed from?

    The user basically can type in a url/some Windows app name (like notepad or calc) in the textbox, and it will execute/create that process. But I would also like to kill it

    but how can I do this, since the value is being retrieved from a textbox on the form and that is not a proper "process" type object

     

    Monday, October 31, 2005 5:26 AM

Answers

  • Hi,

    I tried the same code that you have provided and the Kill method worked fine.
    The Close method had no effect. In both cases, I did not get any exceptions.
    I ran the code on a machine running Windows Server 2003.

    Regards,
    Vikram
    Monday, October 31, 2005 6:23 AM
  • Try to (re)get your process and then kill it by something like:




    System.Diagnostics.Process[] myProcesses = Process.GetProcessesByName("YourProcName"
    );

    for (int i = 0; i < myProcesses.Length; i++) {

    if ( /* Chick if it's your right  process */)

    {

    myProcesses [ i ].Kill();

    }

    }




     
    Monday, October 31, 2005 10:16 AM

All replies

  • Hi,

    I tried the same code that you have provided and the Kill method worked fine.
    The Close method had no effect. In both cases, I did not get any exceptions.
    I ran the code on a machine running Windows Server 2003.

    Regards,
    Vikram
    Monday, October 31, 2005 6:23 AM
  • Try to (re)get your process and then kill it by something like:




    System.Diagnostics.Process[] myProcesses = Process.GetProcessesByName("YourProcName"
    );

    for (int i = 0; i < myProcesses.Length; i++) {

    if ( /* Chick if it's your right  process */)

    {

    myProcesses [ i ].Kill();

    }

    }




     
    Monday, October 31, 2005 10:16 AM
  • Thanks very much for your response, much appreciated

    I will try that code, however I can see potential issues with that code:

    since I do not know what the user will be typing in the textbox, it is hard for me to look at what process was executed (procname)

    I will try it with some test code/inputs and see where that takes me

    Vikram: I also am running it on a WS 2003 EE system, as well as Windows XP PRO SP2

     

    Monday, October 31, 2005 10:45 AM
  • Hi,

    Try running the code once as Administrator and check if it helps.

    You can always store the value entered by the user onto a class level variable and then later re-use it to kill the process.

    Regards,
    Vikram
    Monday, October 31, 2005 10:49 AM
  • Alternatively you could temporary save user input in (xml) file / registry and so on..
    Monday, October 31, 2005 10:49 AM
  • ok, i noticed some things

     

    if I run "iexplore" or "wmplayer", as an example, for the Process, I get a specified path not found exception (however works perfect in .NET 2.0), which is wierd since it seems to understand the Windows command "Notepad" fine. The links to wmplayer or iexplore are fine, so I know the OS knows what these sys apps are.

    away from that....

    if the user just enters a url (to a streaming audio for example, or just a website) im the text box, user clicks a button to execute that process - how can I kill that process afterwords - since the text retrieved is from a textbox and not an actual application? (which technically it will execute)

    Monday, October 31, 2005 10:57 AM
  • Vikram: I am as admin ;)
    Monday, October 31, 2005 10:57 AM
  • Hi,

    OK, I tried with Notepad when I claimed that it worked.

    In the scenario that you are describing, you would need to retrieve the associated program based on the "file extension" from the Windows registry and then kill the application based on that.

    Then again, if there are multiple instances of the same app open, such as a Windows Media Player - then its not possible to detect which one to close!

    Regards,
    Vikram
    Monday, October 31, 2005 11:06 AM
  • Hm.

    Very strange problem. Could it be that the program named in textbox text is one that forks it's own processes and then closes?

    I don't know if Process.Kill can get to kill any child processes, but it seems very unlikly.

    Have you tried specifying notepad.exe or calc.exe in your textbox?
    If it does not work, it is hard to believe we are talking about the same someProcess object, but maybe two different objects with the same name...

    Have you tried placing all the code in a single method? say, with a sleep between them?

    Happy processing
    Gorm

    // Reget..
    > System.Diagnostics.Process[] myProcesses = Process.GetProcessesByName("YourProcName");
    > for (int i = 0; i < myProcesses.Length; i++)
    > if ( /* Check if it's your right process */ )
    > myProcesses [ i ].Kill();

    How can you "Check if it's your right process"?

    Monday, October 31, 2005 11:06 AM
  • > How can you "Check if it's your right process"?


    if(myProcesses [ i ].MainWindowTitle == "mywindowTitle") ....

    or through other process properties..

    Monday, October 31, 2005 11:10 AM
  • Gorm: I did try that in a single method and a sleep delay of 2 seconds (and longer) but that did not seem to make a difference.

    I actually tried that before making this thread :-)

     

    Now, I have just done more testing and noticed this:

    if I give a process some arguments (like IE to open a webpage) it does not like it and gives me a "specified path not found". This input for the argument results in this error with the argument given from the user (textbox input) or even if i manually put it in the code

    this is even when using the arguements property in the Process.StartInfo object

    ??

    Monday, October 31, 2005 11:16 AM
  • Try to give the full path or better, define the StartInfo.WorkingDirectory prop
    Monday, October 31, 2005 11:19 AM
  • that seems to work BUT still cannot kill the process :(

    Even thought if I use that, I still would not know what application the user typed in order for me to set that property....

    I could obviously add a validator to check the file extension and appropriatly get the "normal" app directory to execute that link/app entered but the user can set a different directory for the application when they installed it

    I may just set it to use WMP (since the user will enter the url of the audio stream) but I would still need to know where WMP would be installed, sure, by default it is in program files\Windows Media Player but that directory may be changed by the user

    Thats why I thought just by typing in/giving it the sys app name "wmplayer" and giving it the arguments would be better, then i can find and kill that process, but that does not appear to be working as it does not like to start a process giving it the arguments (like the url to the stream)

     

    Monday, October 31, 2005 11:25 AM
  • A crazy solution: notice the start time of this process. Then, read all processes and find the right one through Process.StartTime property :-)

    if(startTime == myproc.StartTime) myproc.Kill();
    Monday, October 31, 2005 11:36 AM
  • Well i seem to have sorted it! (need to work on the kill thing)

    [code]

    this.someProc = new Process();

    this.someProc.StartInfo.Arguments = this.textBox1.Text;

    this.someProc.StartInfo.FileName = "iexplore";

    this.someProc.Start();

    [/code]

    THAT seems to work now, which is wierd! so I give it the arguments, and the sys app name, and seems to work perfectly.

    Now i need to figure out how to kill that process, since the name of the file maybe a Realplayer file or a WMP player or some other player...

    Monday, October 31, 2005 11:40 AM
  • OK, to kill it, thought I would get the threads created by that process, then for each thread process, get the ID and kill it. Or just about the thread.

    However since kill does not take in a parameter to kill a process ID, or even to abort it, I am pretty much stuck at the minute

    I have noticed though, when I execute the process, it creates (apperently using the Count property) 11-12 process threads!!

    Monday, October 31, 2005 11:49 AM
  • > like IE to open a webpage

    Aha. Internet Explorer.
    Have you tried to do a Process.WaitFor (to check if the process just activates an existing iexplore.exe and then exists, like I mentioned in my first post). If it is ending almost immediatly, I believe it is reassigning its work to another process.

    I think "Launch folder windows in a seperate process" (in Folder Options) might have something to do with this. I think this is the case for WINAPI ShellExecute, anyway.

    You might have seen that running a webpage in Start->Run can change an existing iexplore.exe, not open a new one. You have started a new process, I think, it's just short lived.

    Try running "cmd /Cnotepad.exe" and kill the command window. How will you (programatically) kill the notepad window? My guess is that you can't.
    Monday, October 31, 2005 2:50 PM
  • Regards.

    > since the user will enter the url of the audio stream

    To play sound I would, for a power user:

    Read the key: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MediaPlayer\Installation Directory]
    append "\wmplayer.exe"
    You have your executable name.

    OR for a say, terminal services user:

    Read regkey: HKEY_CURRENT_USER\Software\Microsoft\MediaPlayer\Setup\CreatedLinks\AppName

    OR maybe

    Consider WINAPI shell32.dll FindExecutable.

    OR if I don't know the playing application:

    Read and parse regkey: HKEY_CLASSES_ROOT\mp3file\shell\open\command
    for instance

    OR

    something else not doing any process killing.

    Happy media playing

    Monday, October 31, 2005 9:32 PM
  • lol. thanks for that! :-)

    I will see what I can do. I got it all to work but just need to kill the process. I prefer not to use registry keys etc.. for a lot of reasons however it would be interesting to see that approach working :)

    since I have solved the problem of launching a process, depending on the media file (real player, launch realplayer, wma/mp3, launch WMP etc...) I think I am able to now just call it using a normal sys command (like wmplayer, mplayer2, iexplore etc...) and then i would be able to kill the process.

    Anyway... I am going to try both solutions

    thanks alot Gorm, and the rest, you have all been great!!!!
    Tuesday, November 1, 2005 2:27 AM
  • I apologize for the blast for the past, but I got pointed here when a friend was doing some research. =)

    Probably better than looking "Installation Directory" or "Installation DirectoryLFN" up would be to use the AppPaths value for the EXE in question, which in this case would be
      HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\wmplayer.exe

    Cheers,
    -Zach
    Thursday, June 4, 2009 8:19 PM