簡體   English   中英

C#流程DataReceivedEventHandler僅工作一次

[英]C# Process DataReceivedEventHandler works only once

我有一個Unity應用程序,該應用程序創建一個嘗試通過adb shell將命令發送到Android設備的進程。 我可以在應用程序過程中發送命令。 但是我只在第一次收到來自Android設備的輸出。 我不知道它的OutputDataReceived是否存在緩沖區問題。 輸入通過,但在Unity編輯器上沒有收到任何日志。 如果將Process.StartInfo.CreateNoWindow設置為true,則可以看到在Unity外部生成的adb shell中的所有輸出。 第一次執行后,輸出不會從該控制台重定向到Unity。

    public void StartShellProcessAsync ()
    {
        ShellThread = new Thread(() =>
        {
            ShellProcess = new Process();
            ShellProcess.StartInfo.FileName = "adb.exe";
            ShellProcess.StartInfo.Arguments = "shell";

            ShellProcess.StartInfo.CreateNoWindow = false;
            ShellProcess.StartInfo.UseShellExecute = false;

            ShellProcess.StartInfo.RedirectStandardOutput = true;
            ShellProcess.StartInfo.RedirectStandardError = true;
            ShellProcess.StartInfo.RedirectStandardInput = true;

            ShellProcess.EnableRaisingEvents = true;
            ShellProcess.OutputDataReceived += new DataReceivedEventHandler(OnOutputDataReceived);
            ShellProcess.ErrorDataReceived += new DataReceivedEventHandler(OnErrorDataReceived);

            ShellProcess.Exited += new EventHandler(OnProcessExited);

            ShellProcess.Start();
            ShellProcess.BeginOutputReadLine();
            ShellProcess.BeginErrorReadLine();
            ShellProcess.WaitForExit();
        });
        ShellThread.Start();
    }

我在Unity中有發送如下命令的按鈕:

am start -m <application activity>
am stack list

我正在嘗試從am stack list讀取輸出,如果它是我發出的第一個命令,它將起作用。 但是,如果我在am start -m <application activity>之后運行它,則輸出不會傳遞到Unity。

我在線程內運行ShellProcess的原因是使該進程在應用程序的整個執行過程中保持打開狀態。 我不想為我發送給Android的每條命令都耗時而產生新的過程。

嘗試鎖定線程。 這樣可以確保一次僅激活一個進程。

如果在另一個線程正忙於執行同一進程的同時調用一個新線程,則會產生一些沖突。

private readonly object balanceLock = new object();

//Place this code elsewhere
private void Instantiate_Shell_Once()
{

        ShellProcess = new Process();
        ShellProcess.StartInfo.FileName = "adb.exe";
        ShellProcess.StartInfo.Arguments = "shell";

        ShellProcess.StartInfo.CreateNoWindow = false;
        ShellProcess.StartInfo.UseShellExecute = false;

        ShellProcess.StartInfo.RedirectStandardOutput = true;
        ShellProcess.StartInfo.RedirectStandardError = true;
        ShellProcess.StartInfo.RedirectStandardInput = true;

        ShellProcess.EnableRaisingEvents = true;
        ShellProcess.OutputDataReceived += new DataReceivedEventHandler(OnOutputDataReceived);
        ShellProcess.ErrorDataReceived += new DataReceivedEventHandler(OnErrorDataReceived);

        ShellProcess.Exited += new EventHandler(OnProcessExited);

}

public void StartShellProcessAsync ()
{
    ShellThread = new Thread(() =>
    {
        lock(balanceLock)
        {

        ShellProcess.Start();
        ShellProcess.BeginOutputReadLine();
        ShellProcess.BeginErrorReadLine();
        ShellProcess.WaitForExit();

       }
   });
   ShellThread.Start();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM