可能性の重複:
How to Run a C# console application with the console hidden.C#でコンソールアプリケーションを実行しますか?
私が処理して行って、私の方法を教え、その後、タスクを実行するために、バックグラウンドでコンソールアプリケーションを実行してくださいどのようにクラス・ライブラリを経由して?
可能性の重複:
How to Run a C# console application with the console hidden.C#でコンソールアプリケーションを実行しますか?
私が処理して行って、私の方法を教え、その後、タスクを実行するために、バックグラウンドでコンソールアプリケーションを実行してくださいどのようにクラス・ライブラリを経由して?
これにはProcessクラスを使用できます。ここで
は一例です:
// 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();
プロセスが完了したかどうかを確認するHasExitedプロパティもあります。
あなたはこのようにそれを使用することができます:
if (p.HasExited)...
か、Exitedイベントにイベントハンドラをバインドすることができます。
あなたのアプリの実行が完了するのを待っているのであれば、Shekharの答えのようにproc.WaitForExit()を使用してください。バックグラウンドで実行し、待機しない場合は、Exitedイベントを使用します。
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
}
私はここに手足に出て行くよ、私はあなたが完全にコンソールアプリケーションのウィンドウを隠す意味だと思います。
この場合、いくつかのP/Invokingを通じて達成できます。
私は嘘をついた。投稿した元のコードはトレイの「X」ボタンを無効にするだけです。混乱のため申し訳ありません...
WinForms.ShowWindow(consoleWindow, NativeConstants.SW_HIDE)
[DllImport("user32.dll")]
public static extern Boolean ShowWindow(IntPtr hWnd, Int32 show);
そして、ここでP /呼び出し文:
/// <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);
オリジナルコード
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));
}
}
}
そして、コンソールアプリケーションで私はあなたがDOSウィンドウの実行可能ファイルを意味すると仮定? –
あなたはそれが非同期に戻るか、アプリが終了するのを待っていますか? – Joe
001の場合、人々があなたに「受け入れられた」回答をさらに表示するようにしてください。これはあなたの質問に遭遇し、またあなたを助けてくれる人に正当な信用を与える他の人に役立ちます。 –