user32의 쇼 와인도는 예상대로 작동하지 않습니다
-
22-07-2019 - |
문제
user32의 Showwindow 메소드를 사용하여 사용자로부터 창 (CMD.exe)을 숨기고 있습니다 (주로 닫히지 않도록). 사용자가 양식을 열면 프로세스가 시작되고 숨겨지면 양식이 폐쇄되면 프로세스가 죽습니다. 그러나 양식이 다시 열리면 창을 숨기지 않고 (때로는 처음에는 그렇지 않습니다) 누군가가 나를 도와 줄 수 있습니까?
[DllImport("User32")]
private static extern int ShowWindow(int hwnd, int nCmdShow); //this will allow me to hide a window
public ConsoleForm(Process p) {
this.p = p;
p.Start();
ShowWindow((int)p.MainWindowHandle, 0); //0 means to hide the window. See User32.ShowWindow documentation SW_HIDE
this.inStream = p.StandardInput;
this.outStream = p.StandardOutput;
this.errorStream = p.StandardError;
InitializeComponent();
wr = new watcherReader(watchProc);
wr.BeginInvoke(this.outStream, this.txtOut, null, null);
wr.BeginInvoke(this.errorStream, this.txtOut2, null, null);
}
private delegate void watcherReader(StreamReader sr, RichTextBox rtb);
private void watchProc(StreamReader sr, RichTextBox rtb) {
string line = sr.ReadLine();
while (line != null && !stop && !p.WaitForExit(0)) {
//Console.WriteLine(line);
line = stripColors(line);
rtb.Text += line + "\n";
line = sr.ReadLine();
}
}
public void start(string[] folders, string serverPath) {
this.inStream.WriteLine("chdir C:\\cygwin\\bin");
//this.inStream.WriteLine("bash --login -i");
this.inStream.WriteLine("");
}
private void ConsoleForm_FormClosed(object sender, FormClosedEventArgs e) {
this.stop = true;
try {
this.p.Kill();
this.p.CloseMainWindow();
} catch (InvalidOperationException) {
return;
}
}
해결책
이것에 훨씬 더 쉬울 것입니다.
public ConsoleForm(Process p) {
this.p = p;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
p.Start();
this.inStream = p.StandardInput;
this.outStream = p.StandardOutput;
this.errorStream = p.StandardError;
InitializeComponent();
wr = new watcherReader(watchProc);
wr.BeginInvoke(this.outStream, this.txtOut, null, null);
wr.BeginInvoke(this.errorStream, this.txtOut2, null, null);
}
다른 팁
당신은 있는지 확인 했습니까? p.MainWindowHandle
유효한 핸들입니까? 최소한 0이 아닌 것이어야합니다. 전화 해보세요 IsWindow
확인.
MSDN이 제안합니다 부름 WaitForInputIdle
확인하기 전에 MainWindowHandle
; 새 프로세스가 창을 만들기 전에 속성에 액세스 할 수 있습니다. 프로세스에는 실제로 "기본"창에 대한 개념이 없기 때문에 속성은 본질적으로 불안정합니다. 모든 창문은 똑같이 처리됩니다. .NET 프레임 워크는 단순히 첫 번째 창을 기본 창으로 지정하지만 프로세스 자체는 그런 식으로 생각할 필요가 없습니다.
또한 프로세스를 시작한 다음 사실을 숨기는 대신 처음에 프로세스를 숨기는 것을 고려 했습니까? 프로세스를 설정하십시오 StartInfo
속성 SCOTTY2012가 보여줍니다.
제휴하지 않습니다 StackOverflow