2015-04-22 26 views
1

所以我得到了一些運行在USB連接事件上的代碼。如果和Android設備被檢測到,它會運行一些構建道具相關的代碼。但是...它只能設法返回Android版本,沒有別的。代碼如下。通過跨線程設置3個不同標籤的多個值c#winforms

 private void Main_Shown(object sender, EventArgs e) 
    { 
     var watcher = new ManagementEventWatcher(); 
     var query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"); 
     watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived); 
     watcher.Query = query; 
     watcher.Start(); 
    } 

    private void watcher_EventArrived(object sender, EventArrivedEventArgs e) 
    { 
     Process process = new System.Diagnostics.Process(); 
     ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
     startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
     startInfo.RedirectStandardInput = false; 
     startInfo.CreateNoWindow = true; 
     startInfo.RedirectStandardOutput = true; 
     startInfo.RedirectStandardError = false; 
     startInfo.UseShellExecute = false; 
     startInfo.FileName = "adb.exe"; 
     startInfo.Arguments = "shell getprop ro.build.version.release"; 
     process = Process.Start(startInfo); 
     device.Invoke((MethodInvoker)(() => device.Text = process.StandardOutput.ReadToEnd())); 

     Process p = new System.Diagnostics.Process(); 
     ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo(); 
     si.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
     si.RedirectStandardInput = false; 
     si.CreateNoWindow = true; 
     si.RedirectStandardOutput = true; 
     si.RedirectStandardError = false; 
     si.UseShellExecute = false; 
     si.FileName = "adb.exe"; 
     si.Arguments = "shell getprop ro.product.model"; 
     p = Process.Start(si); 
     AV.Invoke((MethodInvoker)(() => AV.Text = process.StandardOutput.ReadToEnd())); 

     Process pr = new System.Diagnostics.Process(); 
     ProcessStartInfo siy = new System.Diagnostics.ProcessStartInfo(); 
     siy.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
     siy.RedirectStandardInput = false; 
     siy.CreateNoWindow = true; 
     siy.RedirectStandardOutput = true; 
     siy.RedirectStandardError = false; 
     siy.UseShellExecute = false; 
     siy.FileName = "adb.exe"; 
     siy.Arguments = "shell getprop ro.product.name"; 
     pr = Process.Start(siy); 
     name.Invoke((MethodInvoker)(() => name.Text = process.StandardOutput.ReadToEnd())); 

這應該返回3個值並將它們放入各自的標籤中。但是隻有一個被返回。有什麼辦法可以改進代碼嗎? 和shell getprop ro.*工作在一個較早的版本,並沒有改變...

回答

1

原來我是一個derp。 我對所有內容都設置了相同的進程,並拋出錯誤。菜鳥錯誤真的。我太被我自己炒作了... 新代碼

   Process process = new System.Diagnostics.Process(); 
      ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
      startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
      startInfo.RedirectStandardInput = false; 
      startInfo.CreateNoWindow = true; 
      startInfo.RedirectStandardOutput = true; 
      startInfo.RedirectStandardError = false; 
      startInfo.UseShellExecute = false; 
      startInfo.FileName = "adb.exe"; 
      startInfo.Arguments = "shell getprop ro.build.version.release"; 
      process = Process.Start(startInfo); 
      device.Invoke((MethodInvoker)(() => device.Text = process.StandardOutput.ReadToEnd())); 

      Process p = new System.Diagnostics.Process(); 
      ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo(); 
      si.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
      si.RedirectStandardInput = false; 
      si.CreateNoWindow = true; 
      si.RedirectStandardOutput = true; 
      si.RedirectStandardError = false; 
      si.UseShellExecute = false; 
      si.FileName = "adb.exe"; 
      si.Arguments = "shell getprop ro.product.model"; 
      p = Process.Start(si); 
      AV.Invoke((MethodInvoker)(() => AV.Text = p.StandardOutput.ReadToEnd())); 

      Process pr = new System.Diagnostics.Process(); 
      ProcessStartInfo siy = new System.Diagnostics.ProcessStartInfo(); 
      siy.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
      siy.RedirectStandardInput = false; 
      siy.CreateNoWindow = true; 
      siy.RedirectStandardOutput = true; 
      siy.RedirectStandardError = false; 
      siy.UseShellExecute = false; 
      siy.FileName = "adb.exe"; 
      siy.Arguments = "shell getprop ro.product.name"; 
      pr = Process.Start(siy); 
      name.Invoke((MethodInvoker)(() => name.Text = pr.StandardOutput.ReadToEnd()));