.NET 1.1 process.kill?

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

 




Answer this question

.NET 1.1 process.kill?

  • eszra

    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

  • drammer

    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

     



  • aorozcoc

    Vikram: I am as admin ;)

  • PinkFloyd11

    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();

  • Arpit

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


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

    or through other process properties..



  • Kris Reynolds

    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"



  • Oreng

    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)

     



  • kookoo_dan

    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



  • uitmaurik

    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();

    }

    }




     


  • Sivar

    Alternatively you could temporary save user input in (xml) file / registry and so on..

  • Daniel Goita Ferketa

    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)



  • BlazingFox

    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

  • Asher

    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


  • A.J. Smith

    Try to give the full path or better, define the StartInfo.WorkingDirectory prop

  • .NET 1.1 process.kill?