Solved

c# kill process bat file

Posted on 2014-11-05
5
532 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 74

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 21
Format column on datatable 7 18
C# TextBox 11 16
Make all values in arrayist to titlecase+vb.net 23 13
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

9 Experts available now in Live!

Get 1:1 Help Now