Solved

c# kill process bat file

Posted on 2014-11-05
5
550 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
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 79

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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