Reputation: 11673
I'm using a small C# executable to launch a java jar. I want to recover the exit code returned by the jar so i can restart the jar if i need to. However the c# application keeps showing a black console window and i can't get rid of it, does anyone know how to fix this? I'm using t he following C# code to start the process
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "jre/bin/java.exe";
p.StartInfo.Arguments = "-Djava.library.path=bin -jar readermanager.jar";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.waitForExit();
return p.ExitCode;
The console window only keeps visible when using the waitForExit(); method. Without it (and withoud p.ExitCode) the console windows closes. I also tried setting the StartInfo.WindowStyle to Hidden and Minimized but both don't have any effect on the window.
Upvotes: 1
Views: 1618
Reputation: 7475
From How to run a C# console application with the console hidden
System.Diagnostics.ProcessStartInfo start =
new System.Diagnostics.ProcessStartInfo();
start.FileName = dir + @"\Myprocesstostart.exe";
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
But if that does not work, how about this: http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
IntPtr hWnd = FindWindow(null, "Window caption here");
if(hWnd != IntPtr.Zero)
{
//Hide the window
ShowWindow(hWnd, 0); // 0 = SW_HIDE
}
if(hWnd != IntPtr.Zero)
{
//Show window again
ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
}
Upvotes: 1
Reputation: 109537
Just change the output type of your C# program to be a "Windows Application" instead of a "Console Application". A C# Windows application doesn't really care if you actually display any windows.
Upvotes: 2