读取过程,输出等待特定的输出并立即退出

时间:2019-04-05 05:23:57

标签: c# process

我有const appDiv = document.getElementById('app'); const my_steam_id = 'Rex4748'; const players = [{ steamid: 'Rex4748', avatarfull: 'https://fakeimg.pl/50/', personaname: 'Joe Perez' },{ steamid: 'notYou', avatarfull: 'https://fakeimg.pl/50/', personaname: 'NewGuy' }] function getPlayers(){ let info_collected = []; players.map((player)=> { console.log(player.steamid); info_collected.push(` <div class="player player-${player.steamid}"> <div class="name"> ${player.personaname} </div> <div class="avatar"> <img src="${player.avatarfull}"> </div> <div class="role"> ${player.steamid == my_steam_id ? 'Owner' : 'Moderator'} </div> </div> <hr> `); }) return info_collected.join(''); } appDiv.innerHTML = getPlayers(); 个文件启动了.bat服务器(appium命令行),我是通过appium文件来做到这一点的:

.bat

var process = new Process(); var startinfo = new ProcessStartInfo("cmd.exe", @"/C c:\tools\start_appium_aerver.bat"); startinfo.RedirectStandardOutput = true; startinfo.UseShellExecute = false; process.StartInfo = startinfo; process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data); // do whatever processing you need to do in this handler process.Start(); process.BeginOutputReadLine(); 启动时,我想通过读取appium输出并等待特定的行来了解服务器是否成功启动:

process

此时,当我找到此行并知道我的服务器正在崩溃而不是使用此命令并等待几秒钟时:

appium server start listening

我希望能够立即退出(使用// Wait until appium server starts process.WaitForExit(30000); 而没有任何值是个坏主意,因为服务器一直在运行)

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

看到您要启动服务器的必需文本后,让您的OutputDataReceived处理程序调用{​​{3}}。

如果它是应用程序中唯一活动的前台线程,您仍然可能希望创建Process的代码块使用WaitForExit(有或没有超时),也只是为了阻止您退出尽早(据我所知,OutputDataReceived上的事件处理程序不足以在进程中没有前台线程时保持进程运行)