【问题标题】:Why is manual Thread.Sleeping necessary before Process.Start here?为什么在 Process.Start here 之前需要手动 Thread.Sleeping?
【发布时间】: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 没有延迟也没有问题 -- 为什么我需要它们??

【问题讨论】:

    标签: c# android stream


    【解决方案1】:

    好的,在我的情况下,设备实际上还没有准备好接收命令。我在等待这样一个良好的设备状态。

        ProcessStartInfo lcmdInfo1;
    
        lcmdInfo1 = new ProcessStartInfo(" adb.exe ", "get-state");
        lcmdInfo1.CreateNoWindow = true;
        lcmdInfo1.RedirectStandardOutput = true;
        lcmdInfo1.RedirectStandardError = true;
        lcmdInfo1.UseShellExecute = false;
    
        Process cmd2 = new Process();
        cmd2.StartInfo = lcmdInfo1;
    
        var output = new StringBuilder();
        var error = new StringBuilder();
    
        cmd2.OutputDataReceived += (o, ef) => output.Append(ef.Data);
        cmd2.ErrorDataReceived += (o, ef) => error.Append(ef.Data);
        cmd2.Start();
        cmd2.BeginOutputReadLine();
        cmd2.BeginErrorReadLine();
        cmd2.WaitForExit();
        cmd2.Close();
        lresulterr1 = error.ToString();
        lresult1 = output.ToString();
        cmd2.Dispose();
    
        //sometimes there is an issue with a previously issued command that causes the device status to be 'Unknown'. Wait until the device status is 'device'
        while (!lresult1.Contains("device"))
        { 
            lcmdInfo1 = new ProcessStartInfo(" adb.exe ", "get-state");
            lcmdInfo1.CreateNoWindow = true;
            lcmdInfo1.RedirectStandardOutput = true;
            lcmdInfo1.RedirectStandardError = true;
            lcmdInfo1.UseShellExecute = false;
    
            cmd2 = new Process();
            cmd2.StartInfo = lcmdInfo1;
    
            output = new StringBuilder();
            error = new StringBuilder();
    
            cmd2.OutputDataReceived += (o, ef) => output.Append(ef.Data);
            cmd2.ErrorDataReceived += (o, ef) => error.Append(ef.Data);
            cmd2.Start();
            cmd2.BeginOutputReadLine();
            cmd2.BeginErrorReadLine();
            cmd2.WaitForExit();
            cmd2.Close();
            lresulterr1 = error.ToString();
            lresult1 = output.ToString();
            cmd2.Dispose();
        }
     //now your device is ready. Go ahead and fire off the shell commands
    

    【讨论】:

      猜你喜欢
      • 2015-08-19
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 2020-09-17
      • 1970-01-01
      相关资源
      最近更新 更多