【问题标题】:Running ADB Shell commands rapidly in C# console app在 C# 控制台应用程序中快速运行 ADB Shell 命令
【发布时间】:2014-01-15 18:02:53
【问题描述】:

我正在尝试快速运行大量的 adb shell 命令。基本上,我想启动 adb shell,然后快速连续运行一堆命令。我可以以某种方式重用流程吗?我想启动 adb shell 并在运行时更改命令文本。

问题在于为每个命令创建一个单独的进程会启动大量进程并最终导致 adb 对我产生影响。

    static void Main(string[] args)
    {
        const string AdbBroadcast = "shell am broadcast <my_cmd>";


        int broacastIndex = 0;
        while(true)
        {
            Console.WriteLine("Outputting " + broacastIndex);

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "adb";
            startInfo.Arguments = AdbBroadcast;
            process.StartInfo = startInfo;
            process.Start();

            process.WaitForExit();


            Thread.Sleep(250);
            broacastIndex++;

        }

    }

【问题讨论】:

  • 我不知道这是否有助于解决您的具体错误,但您应该在完成处理后调用 .Dispose (在“WaitForExit”之后)。如果这个循环有很多迭代,你可能会在操作系统级别用完进程句柄。处理过程应释放句柄,以便可以重复使用。

标签: c# shell adb


【解决方案1】:

当设备不响应​​通过进程通过 adb 发出的 shell 命令时,您可以检查设备状态如下:

    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

【讨论】:

    猜你喜欢
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多