c# - Different behaviour when using a command line script or running it from a backgroundworker -


I have a very strange problem that recurs the strong knowledge of the C #'s background player.

I want to run the command line program and check its ERRORLEVEL.

Launch the cmd.exe / c echo command & amp; COMMAND_TO_RUN & amp; Echo Insert State: & amp; Amp; Reverse% ERRORLEVEL% & amp; Amp; % Errorlevel% NEQ 0 (echo failure) and (echo success))

When order is unsuccessful, get ERRORLEVEL value -1

This works when I am Cmd. Use this line in the exe.

But, here's the problem.

When I used to run this as a background worker line, then ERRORLEVEL 0 assume COMMAND_TO_RUN fail

Here's my BackgroundWorker code:

  string [] args = {"echo resonance" in order & amp; amp;;; COMMAND_TO_RUN & amp; connection statut echo: & amp; echo% errorlevel% & amp; if% errorlevel% NEQ 0 (echo failure) and (success corresponds))}; backgroundWorker1.RunWorkerAsync (args); private void BackgroundWorkerl_DoWork (object sender, DoWorkEventArgs e) {string [] args = e.Argument string [] ;) System.Di agnostics.Process process = new System.Diagnostics.Process (; process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = "cmd.exe"; process.StartInfo .Arguments = args [0]; Process.start.info. CreateNoWindow = true; Process.start.info.RedirectStandardInput = false; Process.start.info.RedirectStandardOutput = true; Process.start.info.RedirectStandardError = false; process.Start (); System.IO.StreamReader sOut = process.StandardOutput ; String temp; While (. SOut.EndOfStream) {// report progress using userstate, I allow programmatically output / capture in runtime tempOut = sOut.ReadLine (); Background Type 1 Report progress (1, tempOut); } process. Stop it ();         

Know anything about the background worker (sorry) There are at least three problems with the command part:

  1. Based on the real COMMAND_TO_RUN , maybe this cmd cmd example has ended, execution of a problem without it, so that you get in your C # code Error level, from CMD Everyone out of the code, is 0.

  2. When the line is read CMD , it is parsed and then executed. In the purse phase, all the variable read operations are executed with command and before the line is executed the variable is converted to first value. So, reads all of your % errorlevel% (that's all being done in the same line) when you do not change your COMMAND_TO_RUN running and before the command the value of the variable Execution will come back.

  3. A combination problem is A and amp; B is used to add command execution, that is, run A, when expired B but A & amp; Amp; B means run A and if there is no error level then B ( a & amp; amp; amp; b; c; c )

Try something like this ( dir Command which will fail)

  cmd.exe / c "echo Command and Launching (& dir * .notexist & gt; nul 2 & gt; Null) & amp; amp; Ek SUCCESS & amp; exit / B) || & amp; exit; out / b1)) " 

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -