Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

redirect - C# - Realtime console output redirection -


i'm developing c# application , need start external console program perform tasks (extract files). need redirect output of console program. code this one not work, because raises events when new line writen in console program, 1 use "updates" what's shown in console window, without writting new lines. how can raise event every time text in console updated? or output of console program every x seconds? in advance!

i have had similar (possibly exact) problem describe:

  1. i needed console updates delivered me asynchronously.
  2. i needed updates detected regardless of whether newline input.

what ended doing goes this:

  1. start "endless" loop of calling standardoutput.basestream.beginread.
  2. in callback beginread, check if return value of endread 0; means console process has closed output stream (i.e. never write standard output again).
  3. since beginread forces use constant-length buffer, check if return value of endread equal buffer size. means there may more output waiting read, , may desirable (or necessary) output processed in 1 piece. did keep stringbuilder around , append output read far. whenever output read length < buffer length, notify (i event) there output, send contents of stringbuilder subscriber, , clear it.

however, in case writing more stuff console's standard output. 'm not sure "updating" output means in case.

update: realized (isn't explaining doing great learning experience?) logic outlined above has off-by-one bug: if length of output read beginread exactly equal length of buffer, logic store output in stringbuilder , block while trying see if there's more output append. "current" output sent when/if more output available, part of larger string.

obviously method of guarding against (or biggish buffer plus faith in powers of luck) needed 100% correctly.

update 2 (code):

disclaimer: code not production-ready. result of me hacking proof of concept solution needed done. please not use stands in production application. if code causes horrible things happen you, pretend else wrote it.

public class consoleinputreadeventargs : eventargs {     public consoleinputreadeventargs(string input)     {         this.input = input;     }      public string input { get; private set; } }  public interface iconsoleautomator {     streamwriter standardinput { get; }      event eventhandler<consoleinputreadeventargs> standardinputread; }  public abstract class consoleautomatorbase : iconsoleautomator {     protected readonly stringbuilder inputaccumulator = new stringbuilder();      protected readonly byte[] buffer = new byte[256];      protected volatile bool stopautomation;      public streamwriter standardinput { get; protected set; }      protected streamreader standardoutput { get; set; }      protected streamreader standarderror { get; set; }      public event eventhandler<consoleinputreadeventargs> standardinputread;      protected void beginreadasync()     {         if (!this.stopautomation) {             this.standardoutput.basestream.beginread(this.buffer, 0, this.buffer.length, this.readhappened, null);         }     }      protected virtual void onautomationstopped()     {         this.stopautomation = true;         this.standardoutput.discardbuffereddata();     }      private void readhappened(iasyncresult asyncresult)     {         var bytesread = this.standardoutput.basestream.endread(asyncresult);         if (bytesread == 0) {             this.onautomationstopped();             return;         }          var input = this.standardoutput.currentencoding.getstring(this.buffer, 0, bytesread);         this.inputaccumulator.append(input);          if (bytesread < this.buffer.length) {             this.oninputread(this.inputaccumulator.tostring());         }          this.beginreadasync();     }      private void oninputread(string input)     {         var handler = this.standardinputread;         if (handler == null) {             return;         }          handler(this, new consoleinputreadeventargs(input));         this.inputaccumulator.clear();     } }  public class consoleautomator : consoleautomatorbase, iconsoleautomator {     public consoleautomator(streamwriter standardinput, streamreader standardoutput)     {         this.standardinput = standardinput;         this.standardoutput = standardoutput;     }      public void startautomate()     {         this.stopautomation = false;         this.beginreadasync();     }      public void stopautomation()     {         this.onautomationstopped();     } } 

used so:

var processstartinfo = new processstartinfo     {         filename = "myprocess.exe",         redirectstandardinput = true,         redirectstandardoutput = true,         useshellexecute = false,     };  var process = process.start(processstartinfo); var automator = new consoleautomator(process.standardinput, process.standardoutput);  // automatorstandardinputread event handler automator.standardinputread += automatorstandardinputread; automator.startautomate();  // whatever want while process running process.waitforexit(); automator.standardinputread -= automatorstandardinputread; process.close(); 

Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -