Solved

c# kill process bat file

Posted on 2014-11-05
5
578 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 80

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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