Solved

c# kill process bat file

Posted on 2014-11-05
5
541 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 78

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now