forms - C# BackgroundWorker class -



forms - C# BackgroundWorker class -

currently im trying update progress bar if background worker reports something, heres code

form1.cs

namespace ytd { public partial class form1 : form { private main app; public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { } private void button1_click(object sender, eventargs e) { int n; bool isnumeric = int.tryparse(numberbox.text, out n); if (!string.isnullorwhitespace(emailbox.text) && !string.isnullorwhitespace(passbox.text) && !string.isnullorwhitespace(numberbox.text) && isnumeric) { this.app = new main(emailbox.text, passbox.text, n, logbox, statusbar, backgroundworker1); this.app.startmule(); } else { messagebox.show("please fill out form fields", "mulemaker error"); } } } }

and main.cs

namespace ytd.classes { public class main { private string email; private string password; private int number; private richtextbox logbox; private progressbar statusbar; private backgroundworker threadworker; public main(string email, string password, int number, richtextbox logbox, progressbar statusbar, backgroundworker threadworker) { // define variables this.email = email; this.password = password; this.number = number; this.logbox = logbox; this.statusbar = statusbar; this.threadworker = threadworker; } public void startmule() { // set progressbar 100% value statusbar.maximum = this.number; if (!threadworker.isbusy) { threadworker.runworkerasync(); } } private void threadworker_dowork(object sender, doworkeventargs e) { (int = 1; <= 10; i++) { // perform time consuming operation , study progress. messagebox.show("ye"); system.threading.thread.sleep(500); threadworker.reportprogress(i * 10); } } private void threadworker_progresschanged(object sender, progresschangedeventargs e) { statusbar.increment(1); } } }

currently no errors progress bar value not beeing changed. without background worker can update progress bar fine not while doing expensive action.

your posted code not reveal, if registered functions backgroundworker events.

creating new backgrounworker isn't enough. here example:

public class main { public main( ... ) { backgroundworker worker = new backgroundworker() worker.workerreportsprogress = true; // register backgroundworker-events worker.dowork += threadworker_dowork; worker.progresschanged += threadworker_progresschanged; } }

in add-on should tell progressbar rerender.

private void threadworker_progresschanged(object sender, progresschangedeventargs e) { statusbar.increment(1); statusbar.invalidate(true); }

at to the lowest degree might want utilize value have set calling reportprogress(i * 10).

private void threadworker_progresschanged(object sender, progresschangedeventargs e) { statusbar.value = e.progresspercentage; statusbar.invalidate(true); }

c# forms backgroundworker

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -