[英]C# - Capture Output from a Child Window of a new Process
好吧,即時通訊不會擺弄我跳舞。 直截了當,我試圖為C#中的Unreal Development Kit創建一個GUI UnrealScript編譯器。 確切的性質是指更大的東西,但這是發生了什么:
我啟動了一個新線程,該線程啟動了一個新進程,並設置了一個流讀取器,該流讀取器具有一個偵聽器,可以在將輸出寫入控制台窗口時捕獲其輸出。 這不是問題,除了啟動UDK.exe make
它似乎會生成一個子控制台窗口,即實際的編譯器。 如果我將CD
UDK.exe make >> output.txt
到我的UDK/Binaries/Win32
目錄中,並執行命令UDK.exe make >> output.txt
絕對不會寫入,但是會創建文件(當然,編譯器也傾向於運行)
我正在使用(據我所知)線程安全操作來訪問我的輸出文本框,我希望在編譯器運行時將其寫入。
這是我的代碼:
public partial class ProjectEditor : Form
{
delegate void SetTextCallback(string text);
/*** ... other completely unrealted code ... ***/
private void Menu_Project_Compile_JustCompile_Click(object sender, EventArgs e) {
Thread compile = new Thread(
new ThreadStart (
this.Compile
)
);
NewConsoleLine("Starting Compiler New");
compile.Start();
}
////////
private void Compile() {
this.RunWithRedirect(FileMan.getUDK(), " make");
}
void RunWithRedirect(string cmdPath, string args = null) {
var proc = new Process();
proc.StartInfo.FileName = cmdPath;
proc.StartInfo.Arguments = args;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.EnableRaisingEvents = true;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.ErrorDataReceived += proc_DataReceived;
proc.OutputDataReceived += proc_DataReceived;
proc.Start();
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
proc.WaitForExit();
}
void proc_DataReceived(object sender, DataReceivedEventArgs e) {
NewConsoleLine(e.Data);
}
void NewConsoleLine(String text) {
if (this.OutputConsole.InvokeRequired) {
SetTextCallback d = new SetTextCallback(NewConsoleLine);
this.Invoke(d, new object[] { text });
} else {
OutputConsole.Text += text + "\n";
}
}
}
此刻,當我執行要編譯的方法時,我的控制台將Starting Compiler New
編寫“ Starting Compiler New
”行,並顯示一個UDK.exe控制台窗口,並在那里掛着,根本沒有任何輸出。
是否有人對如何調整流光以捕獲實際輸出有任何見解? 我知道這是有可能的,因為我已經看到其他針對UnrealScript的IDE可以達到這種效果(沒有彈出窗口,沒有實時輸出的完整輸出到控制台窗口),我感覺我已經關閉了,但是還沒到...
嘗試運行UDK.com make
。 這將在與您啟動編譯器相同的窗口中運行編譯器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.