【发布时间】:2014-09-19 20:42:54
【问题描述】:
我有一个场景,我正在从 C# 应用程序读取 Android 手机上的文件,方法是使用 adb.exe 进入手机的外壳并使用 C# 应用程序中的进程读取文件。但是,如果我希望它实际工作,我需要在 Process.Start 之前使用 Thread.Sleep。任何想法为什么?
代码如下:
ProcessStartInfo cmdInfo;
string resulterr = "";
string result = "";
cmdInfo = new ProcessStartInfo(" adb.exe ", "shell cat /etc/bluetooth/bt_stack.conf");
cmdInfo.CreateNoWindow = true;
cmdInfo.RedirectStandardOutput = true;
cmdInfo.RedirectStandardError = true;
cmdInfo.UseShellExecute = false;
Process cmd = new Process();
cmd.StartInfo = cmdInfo;
var output = new StringBuilder();
var error = new StringBuilder();
cmd.OutputDataReceived += (o, ef) => output.Append(ef.Data);
cmd.ErrorDataReceived += (o, ef) => error.Append(ef.Data);
//if I don`t have this Thread.Sleep, the error string is "device not found"!!
Thread.Sleep(5000);
cmd.Start();
cmd.BeginOutputReadLine();
cmd.BeginErrorReadLine();
cmd.WaitForExit();
cmd.Close();
resulterr = error.ToString();
result = output.ToString();
cmd.Dispose();
任何想法为什么这适用于线程睡眠但没有它就不起作用?我可以跑
shell cat /etc/bluetooth/bt_stack.conf
从命令行 ad naseaum 没有延迟也没有问题 -- 为什么我需要它们??
【问题讨论】: