我真的非常喜欢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