c# - backgroundWorker and progressChanged not working -
i cant progress bar work! if execute following code bar remains empty if code gets executed reportprogress doesnt seem update anything..:
namespace gpuz_2 { public partial class form1 : form { public form1() { initializecomponent(); gpuzdata test = new gpuzdata { }; //invio l'oggetto al thread backgroundworker backgroundworker1.runworkerasync(test); } private void backgroundworker1_dowork(object sender, doworkeventargs e) { // // e.argument contains whatever sent background worker // in runworkerasync. can cast original type. // gpuzdata argumenttest = e.argument gpuzdata; argumenttest.onevalue = 6; thread.sleep(2000); backgroundworker1.reportprogress(50); argumenttest.twovalue = 3; thread.sleep(2000); backgroundworker1.reportprogress(100); // // now, return values generated in method. // use e.result. // e.result = argumenttest; } private void backgroundworker1_runworkercompleted(object sender, runworkercompletedeventargs e) { // receive result dowork, , display it. gpuzdata test = e.result gpuzdata; this.text = test.onevalue.tostring() + " " + test.twovalue.tostring(); } private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e) { // change value of progressbar backgroundworker progress. progressbar1.value = e.progresspercentage; // set text. this.text = e.progresspercentage.tostring(); } }
}
thanks in advance help
to initialize backgroundworker, must enable progress reporting , hook event handlers:
// enable progress reporting backgroundworker1.workerreportsprogress = true; // hook event handlers backgroundworker1.dowork += backgroundworker1_dowork; backgroundworker1.runworkercompleted += backgroundworker1_runworkercompleted; backgroundworker1.progresschanged += backgroundworker1_progresschanged;
Comments
Post a Comment