Search code examples
c#taskmanager

C# process task manager kill running file


I try to open file using process in C#, but after that file end work.

It is not end from task manager.

I am running this by thread

how i can kill fping.exe from task manager after specific time

Process cmd = new Process();
cmd.StartInfo.WorkingDirectory = currentDirectory;
cmd.StartInfo.FileName = Directory.GetCurrentDirectory() + "/fping.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.Arguments = pingcmd;

cmd.Start();

Solution

  • When you done working with the Process class, you should dispose of it. To dispose of the type directly, call its Dispose method in a try/finally block. To dispose of it indirectly, use a language construct such as using

    Here is example with using

    using(Process cmd = new Process()){
       cmd.StartInfo.WorkingDirectory = currentDirectory;
       cmd.StartInfo.FileName = Directory.GetCurrentDirectory() + "/fping.exe";
       cmd.StartInfo.RedirectStandardInput = true;
       cmd.StartInfo.RedirectStandardOutput = true;
       cmd.StartInfo.CreateNoWindow = true;
       cmd.StartInfo.UseShellExecute = false;
       cmd.StartInfo.Arguments = pingcmd;
    
       cmd.Start();
    }