c# - Static class / Object? How to dispose -


i'm struggeling oop. start process in additional class. process shell , need access shell severel forms , classes write commands , receive output. use events data. here class process.

my class

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");     } 

}

my form class looks like

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();  } 

everytime have make new object in methods, dont want create everytime new object. make 1 time object , access everytime same object. not want make servel processes. need proccess , send , receive everytime data process.

  1. do have make static class?
  2. how can dispose , close process after i'm quit form class?

1: not want static class. want singleton - class has 1 instance. accessed using static property. @ easiest way works this:

public class () { private () {}  public static instance {get; } = new a(); } 

access via:

a.instance 

2: not. processes not disposed. exit last thread not background thread process ends. otherwise kill it, if has done "in force" outside.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -