Solved

c# kill process bat file

Posted on 2014-11-05
5
603 Views
Last Modified: 2014-11-05
Hi,

I start a bat file "transfert.bat" by using a new process. It works but i would like t o cancel operation.
So what i did is to keep a reference to the launched process and then kill it.
By doing this, the process is not killed... the bat file still continues to transfert data.

Here is the snippet :

  DataReceivedEventHandler handler = (s, e) => AsperaConnector_OnInformation(e.Data);
                        if (_process != null)
                        {
                            _process.OutputDataReceived -= handler;
                            if (!_process.HasExited)
                            {
                                _process.Kill();
                            }
                        }

                        _process = new Process();

                        ProcessStartInfo startInfo = new ProcessStartInfo
                        {
                            WindowStyle = ProcessWindowStyle.Hidden,
                            FileName = "cmd.exe",
                            RedirectStandardOutput = true,
                            RedirectStandardError = true,
                            UseShellExecute = false,
                            Arguments = "/c" + transfertShellFilePath,
                            CreateNoWindow = true
                        };

                        _process.StartInfo = startInfo;

                        _process.OutputDataReceived += handler;

                        _process.Start();
                        _process.BeginOutputReadLine();
                        _process.WaitForExit();


Code to kill :

_process.Kill();
_process.WaitForExit();          


Thanks for the help.

Kind Regards.
0
Comment
Question by:Dnx_7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 40423482
If the .bat file launch another program to make the transfer, and if that program has started doing its work, then killing the batch file will not kill the other program, and the transfer will continue.

Instead of launching the batch file from your code, why don't you code everything in your own application?
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 40423575
Because i have two lines, can i deal without the bat files?

eg :

set ENV_VAR=username
"C:\Users\dnguyen\AppData\Local\Programmes\Aspera\Aspera Connect\bin\ascp.exe" -L -P 33001 -O 33001 -T --policy=fair -U  2 -l 10000 -k 2  -M 65189 -u 6ddd5c1a-0acd-45c6-9e2a-0911f2123dff --ignore-host-key --user="" --host="" --mode=send "./"
0
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 40423666
then you need to kill ascp.exe
taskkill /in ascp.exe /f
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 40423853
can i deal without the bat files?
Sure. You're already using a ProcessStartInfo object, so just modify the EnvironmentVariables property thus:

startInfo.EnvironmentVariables.Add("ENV_VAR", "whatever is in username in the batch file");

Open in new window


Of course, you'd change your original code to use what's in the 2nd line of your batch file, rather than pointing to the batch file itself.
0
 
LVL 2

Author Closing Comment

by:Dnx_7
ID: 40424192
Thanks to you all!
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question