开发者

Execute a console application in c#? [duplicate]

开发者 https://www.devze.com 2023-02-09 20:36 出处:网络
This question already has answers here: 开发者_运维技巧 Closed 12 years ago. Possible Duplicate: How to Run a C# console application with the console hidden.
This question already has answers here: 开发者_运维技巧 Closed 12 years ago.

Possible Duplicate:

How to Run a C# console application with the console hidden.

via a class library how do I run a console application in the background to perform a task, then tell my method its done processing?


You can use Process class 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 process has completed.

you can use it like this:

if (p.HasExited)...

or you can bind an EventHandler to Exited event.


If you are ok with waiting for your app to finish running, use proc.WaitForExit() as in Shekhar's answer. If you want it to run in the background and not wait, use the Exited event.

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

proc.Exited += ProcessExitedHandler;

proc.Start();

And when it's done, you can check for error codes:

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


I'm going out on a limb here, but I think you mean hide the console application window altogether.

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

I lied. The original 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 statements 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));
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号