我真的非常喜歡OOP。我想在我的附加課程中開始一個流程。這個過程是一個shell,我需要從嚴重的形式和類訪問這個shell來編寫命令並接收輸出。我使用事件來獲取數據。這是我的課程。靜態類/對象?如何處置
我對
public class ADBShell
{
public static string output = String.Empty;
public static Process adbshell = new Process();
public void Start_ADBShell()
{
if (adbshell != null && !adbshell.HasExited)
return;
adbshell = new Process();
adbshell.StartInfo.UseShellExecute = false;
adbshell.StartInfo.FileName = @"D:\adb\adb.exe";
adbshell.StartInfo.Arguments = "shell";
adbshell.StartInfo.RedirectStandardOutput = true;
adbshell.StartInfo.RedirectStandardInput = true;
//adb.StartInfo.RedirectStandardError = true;
adbshell.EnableRaisingEvents = true;
adbshell.StartInfo.CreateNoWindow = true;
//adb.ErrorDataReceived += new DataReceivedEventHandler(adb_ErrorDataReceived);
adbshell.OutputDataReceived += new DataReceivedEventHandler(adbshell_OutputDataReceived);
try { var started = adbshell.Start(); }
catch (Exception ex)
{
Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
}
//adb.BeginErrorReadLine();
adbshell.BeginOutputReadLine();
}
void adbshell_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
output += (e.Data) + Environment.NewLine;
}
public void press_touch(string x, string y)
{
adbshell.StandardInput.WriteLine("input tap " + String.Format("{0} {1}", x, y));
Debug.WriteLine("pressed");
}
類}
我的Form類看起來像
public partial class Form1 : Form
{
private bool _record;
private bool _selecting;
private Rectangle _selection;
//---------------------------------------------------------------------
public Form1()
{
InitializeComponent();
}
//---------------------------------------------------------------------
private void Form1_Load(object sender, System.EventArgs e)
{
ADBShell adbshell = new ADBShell();
adbshell.Start_ADBShell();
}
每次我必須做出一個新的對象在我的方法,但我不希望創建每次都有新的對象。我想讓對象每次訪問一次,並訪問同一個對象。我不想製作絲毫過程。我只需要進程,每次發送和接收數據到這個進程。
- 我必須做一個靜態類嗎?
- 如何在我退出Form類後處理並關閉進程?
1。不,只是傳遞對象。 2.在處理表單時處理'Process'。 –
這裏是你的單例 - 「公共類ADBShell {私有靜態ADBShell實例{get; } = new ADBShell();私人ADBShell(){}}' – Enigmativity