Run console application in C #?

Possible duplicate:
How to start a C # console application with a hidden console.

through the class library, how can I launch a console application in the background to complete a task, and then inform my method about its completed processing?

+3
source share
3 answers

You can use Process for this.

Here is an example:

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "your application path";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();

There is also a HasExited property to check if the process is complete.

you can use it as follows:

if (p.HasExited)...

or you can bind an EventHandler event to an Exited .

+5
source

, proc.WaitForExit(), . , , "".

Process proc =
    new Process
    {
        StartInfo =
        {
            FileName = Application.StartupPath +  @"your app name",
            Arguments = "your arguments"
        }
    };

proc.Exited += ProcessExitedHandler;

proc.Start();

, :

if (proc.ExitCode == 1)
{
    // successful
}
else
{
    // something else
}
+1

I'm going out on a limb here, but I think you generally want to hide the console application window.

In this case, you can achieve this through some P / Invoking.

I lied. The source code I posted just disables the "X" button in the tray. Sorry for the confusion ...

WinForms.ShowWindow(consoleWindow, NativeConstants.SW_HIDE)

    [DllImport("user32.dll")]
    public static extern Boolean ShowWindow(IntPtr hWnd, Int32 show);

And the P / Invoke instructions here:

    /// <summary>
    ///     The EnableMenuItem function enables, disables, or grays the specified menu item.
    /// </summary>
    /// <param name="hMenu"></param>
    /// <param name="uIDEnableItem"></param>
    /// <param name="uEnable"></param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();

    /// <summary>
    ///     The GetSystemMenu function allows the application to access the window menu (also known as the system menu or the control menu) for copying and modifying.
    /// </summary>
    /// <param name="hWnd"></param>
    /// <param name="bRevert"></param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

Original code

    private static IntPtr hWndConsole = IntPtr.Zero;
    private static IntPtr hWndMenu = IntPtr.Zero;

    public static void Main(string[] args)
    {
        hWndConsole = WinForms.GetConsoleWindow();
        if (hWndConsole != IntPtr.Zero)
        {
            hWndMenu = WinForms.GetSystemMenu(hWndConsole, false);

            if (hWndMenu != IntPtr.Zero)
            {
                WinForms.EnableMenuItem(hWndMenu, NativeConstants.SC_CLOSE, (uint)(NativeConstants.MF_GRAYED));
            }
        }
    }
+1
source

Source: https://habr.com/ru/post/1791100/


All Articles