backgroundworker - Background worker within a function in c# wpf -


I have made a simple GUI in C # YPF (sorry I can not show a GUI because my reputation is below 10).

This includes rich text boxes and some other controls umm ... read this application a file When reading the file while using a background worker, the file content will appear in the Rich Textbox line from the line. The function that reads the file is like this:

  public int parse_persoFile2 (string fname, backgroundWorker worker, DoWorkEventArgs e) {if (fname == null) return -1; System.IO.StreamReader ifs; Ifs = new system. IO.StreamRender (FNN); Int max = (int) e.Argument; Int p = 0; While (ifs.Peek ()! = -1) {string tempData = ifs.ReadLine (); If (tempData.Contains ("CMD = 5107") || tempData.Contains ("CMD = 5106") || tempData.Contains ("CMD = 5102")) {//field.AppendText (tempData.Remove (tempData final Index ('\\'). Remove (0, 4) + "\ r \ n"); //field.AppendText("--------\r\n "); //System.Threading .hread.Sleep (500); String Data = tempData.Remove (tempData.LastIndexOf ('\\')). Remove (0, 4) + "\ r \ n"; Worker. Report Progress (P, Data); } P ++;} Employee report progress (100); return 0;}  

As we can see, I am using the background worker in this function so that the string Read from file To be displayed in Rich Textbox then send that string to report. As a note that persoFile2 function is made from any other object in my program ...: -)

Then for the rest, I have created a Doorker function, worker_progressChanged, and worker_RunWorker so that the backendworker could work properly. Those codes are as follows:

  Private Wide Dover (Object Sender, Dvover Evlogs E) {Backgroundworker wrk = Sender as Backgraver; Parser.parse_persoFile2 (filename, wrk, e); } Private Zero proggChanged (Object Sender, ProgressChangedEventArgs e) {if (e.UserState! = Null) mRTB.AppendText (e.UserState.ToString ()); } Private Zero Complete (Object Sender, Runwalker, Collected Entente E) {Message Box. Show ("OK ...."); }  

Umm .... When I run this program, it seems that my richextbox does not print the string line from the file line, Prints. ..: -3, .. no, this is my real problem here. I have read this article, but still do not know ....: 3

If you Calling report progression method Very fast, it is possible to properly update the UI thread as "promotion" process and "code"> backgroundwire before hitting it again There will be no chance.

Private Zero Work (Object Sender, DoWorkEventArgs E) {var wrk = BackgroundWorker as sender; // Clearly you will not really do this :) while (true) wrk. ReportProgress (0); }

To see the expectation you are looking for, you can set an artificial "break" in your DoWork event so that UI time Properly update:

  Private zero jobs (Object sender, DoWorkEventArgs e) {var wrk = background by sending; Var p = 0; While (true) {wrk. Report Progress (P ++); Thread.Sleep (100); }}  

For your situation, if the code is executing quickly, you may not need to execute it in a separate thread.

Alternatively, if you say "please wait", "loading ..." to update your UI, you need to do it in BackgroundWorker , And return the end result back to the UI


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -