c# - BeginInvoke cause application hang in BackgroundWorker -


i trying solve problem in question ended having problem
in short words question asking how load huge file textbox chunk chunk,
in ground worker do_work event did this:

using (filestream fs = new filestream(@"myfilepath.txt", filemode.open, fileaccess.read)) {     int buffersize = 50;     byte[] c = null;             while (fs.length - fs.position > 0)     {         c = new byte[buffersize];         fs.read(c , 0,c.length);                             richtextbox1.appendtext(new string(unicodeencoding.ascii.getchars(c)));     }                 }         

that didn't work because backgroundworker can't affect ui elements , need use begininvoke it.

so changed code:

delegate void addtextinvoker();  public void addtext() {                 using (filestream fs = new filestream(@"myfilepath.txt", filemode.open, fileaccess.read))     {         int buffersize = 50;          byte[] c = null;                     while (fs.length - fs.position > 0)         {             c = new byte[buffersize];             fs.read(c , 0,c.length);                                 richtextbox1.appendtext(new string(unicodeencoding.ascii.getchars(c)));         }                      }            } private void worker_dowork(object sender, doworkeventargs e) {     this.begininvoke(new addtextinvoker(addtext)); } 

there 2 problems code.
1- it's taking longer , longer time append text (i think because of string immutability replacing text on time take longer)
2- on every addition richtextbox scroll down end causing application hang.

the question can stop scrolling , application hang?
, can enhance string concatenation here?

edit: after testing , using matt's answer got this:

public void addtext() {                 using (filestream fs = new filestream(@"myfilepath.txt", filemode.open, fileaccess.read))     {         int buffersize = 50;          byte[] c = null;                      while (fs.length - fs.position > 0)         {            c = new byte[buffersize];            fs.read(c , 0,c.length);             string newtext = new string(unicodeencoding.ascii.getchars(c));            this.begininvoke((action)(() => richtextbox1.appendtext(newtext)));             thread.sleep(5000); // here           }                      }          } 

when loading pauses can read , write without problems or hanging, once text exceeded the richtextbox size loading scroll down , prevent me continue.

one problem see background worker is, well, not doing work in background. it's running on ui thread. may why ui thread non-responsive.

i refine dowork handler so:

public void addtext() {                 using (filestream fs = new filestream(@"myfilepath.txt",                     filemode.open, fileaccess.read))     {         int buffersize = 50;          byte[] c = null;                     while (fs.length - fs.position > 0)         {             c = new byte[buffersize];             fs.read(c , 0,c.length);              string newtext = new string(unicodeencoding.ascii.getchars(c));             this.begininvoke((action)(() => richtextbox1.appendtext(newtext));         }                      }            }  private void worker_dowork(object sender, doworkeventargs e) {     addtext(); } 

what i've done localized use of begininvoke single ui call made in handler. way, of other work done in background thread. maybe ui thread becoming non-responsive.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -