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

Raizyl

I have a really weird problem which recquire a strong knowledge of C# 's backgroundworker.

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

cmd.exe /c echo Launching the command && COMMAND_TO_RUN & echo Connexion statut : && echo %ERRORLEVEL% && if %ERRORLEVEL% NEQ 0 (echo FAILURE) ELSE (echo SUCCESS))

When the command fail, the ERRORLEVEL get the value -1

This works when I use this line in the cmd.exe.

But, here is the problem.

When I use a background worker to run this line, the ERRORLEVEL take the value 0 even if the COMMAND_TO_RUN fail.

Here is my backgroundworker code :

string[] args = { "/c echo Launching the command && COMMAND_TO_RUN  & echo Connexion statut : && echo %ERRORLEVEL% && if %ERRORLEVEL% NEQ 0 (echo FAILURE) ELSE (echo SUCCESS))};
backgroundWorker1.RunWorkerAsync(args);


 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string[] args = e.Argument as string[];
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments = args[0];
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.RedirectStandardInput = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = false;
        process.Start();
        System.IO.StreamReader sOut = process.StandardOutput;

        string tempOut;
        while (!sOut.EndOfStream)
        {
            // using the report progress userstate allow me to catch 
            // the program output in runtime
            tempOut = sOut.ReadLine();
            backgroundWorker1.ReportProgress(1, tempOut);
        }
        process.Close();
    }

So Does the use of a backgroundworker lead to a particular value of the ERRORLEVEL ?

Thanks in advance

MC ND

Without knowing anything about BackgroundWorker (sorry), there are at least three problems with the command part:

  1. Depending on the real COMMAND_TO_RUN, maybe it is not executed inside the cmd spawned instance and, independtly of it, the spawned cmd instance has ended its execution without problems so the errorlevel you get in your C# code, the exit code from cmd, can be 0.

  2. When the line is readed by cmd, it is parsed and then executed. In the parse phase, all the variable read operations are removed from the command to be executed and replaced by the value inside the variable before starting to execute the line. So, all your %errorlevel% reads (that are all in the same line) do not change when you run your COMMAND_TO_RUN and will return the value in the variable before the command execution.

  3. There is a concatenation problem. A & B is used to concatenate command execution, that is, run A, when finished run B. But A && B means run A and if not errorlevel then run B (A && B || C means run A, if not errorlevel run B, else run C)

Try with something like this (dir is the command that will fail)

cmd.exe /c "echo Launching the command & (( dir *.notexist >nul 2>nul  )&&( echo SUCCESS & exit /b 0 )||( echo FAILURE & exit /b 1 ))"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Command showing different behaviour when running from terminal and when Using as Shortcut

From Dev

Command showing different behaviour when running from terminal and when Using as Shortcut

From Dev

Different behaviour when running exe from command prompt than in windows

From Dev

Prevent figures from closing when running a script from the command line

From Dev

Download file as csv when running script from command line

From Dev

setClass not found when running R script from command line

From Dev

setClass not found when running R script from command line

From Dev

No return on command line when running Ruby script

From Dev

Script running in PyCharm but not from the command line

From Dev

How do I fix "ReferenceError: System is not defined" when running script from command line?

From Dev

Simple PowerShell script refuses to write file when running from command line but works fine in ISE

From Dev

linux command has different behaviour in script

From Dev

Running an R script from command line (to execute from python)

From Dev

command not found error on Mac when running mfp from command line

From Dev

What is the advantage of running python script using command line?

From Dev

Running a Sqlite3 Script from Command Line

From Dev

Send commands from command line to a running Python script in Unix

From Dev

Running arbitrary vim commands from bash command line to script vim

From Dev

Running a Sqlite3 Script from Command Line

From Dev

Response from Google is different when using PHP curl vs. curl from command line

From Dev

"pymysql.err.InternalError: (1046, u'No database selected')" error message when running Python script from command line

From Dev

How to load different configs for bash script from command line arguments?

From Dev

Error when running JUnit Tests from DOS Command line

From Dev

Ignoring single PHPCS rule when running from command line

From Dev

Java: Redirecting inner process output when running from command line

From Dev

JVM variables escape equals when running from command line

From Dev

Passing array as command line arguments when running exe from batch

From Dev

Unknown command error in Jenkins when running Cake script using Powershell

From Dev

Getting the functionality of the scrapy crawl command when running a spider from a script

Related Related

  1. 1

    Command showing different behaviour when running from terminal and when Using as Shortcut

  2. 2

    Command showing different behaviour when running from terminal and when Using as Shortcut

  3. 3

    Different behaviour when running exe from command prompt than in windows

  4. 4

    Prevent figures from closing when running a script from the command line

  5. 5

    Download file as csv when running script from command line

  6. 6

    setClass not found when running R script from command line

  7. 7

    setClass not found when running R script from command line

  8. 8

    No return on command line when running Ruby script

  9. 9

    Script running in PyCharm but not from the command line

  10. 10

    How do I fix "ReferenceError: System is not defined" when running script from command line?

  11. 11

    Simple PowerShell script refuses to write file when running from command line but works fine in ISE

  12. 12

    linux command has different behaviour in script

  13. 13

    Running an R script from command line (to execute from python)

  14. 14

    command not found error on Mac when running mfp from command line

  15. 15

    What is the advantage of running python script using command line?

  16. 16

    Running a Sqlite3 Script from Command Line

  17. 17

    Send commands from command line to a running Python script in Unix

  18. 18

    Running arbitrary vim commands from bash command line to script vim

  19. 19

    Running a Sqlite3 Script from Command Line

  20. 20

    Response from Google is different when using PHP curl vs. curl from command line

  21. 21

    "pymysql.err.InternalError: (1046, u'No database selected')" error message when running Python script from command line

  22. 22

    How to load different configs for bash script from command line arguments?

  23. 23

    Error when running JUnit Tests from DOS Command line

  24. 24

    Ignoring single PHPCS rule when running from command line

  25. 25

    Java: Redirecting inner process output when running from command line

  26. 26

    JVM variables escape equals when running from command line

  27. 27

    Passing array as command line arguments when running exe from batch

  28. 28

    Unknown command error in Jenkins when running Cake script using Powershell

  29. 29

    Getting the functionality of the scrapy crawl command when running a spider from a script

HotTag

Archive