c# - Form does not displayed as Dialog inside backgroundworker? -
the form not displayed dialog inside backgroundworker?
here code:
//from form1 private void backgroundworkerresult_dowork(object sender, doworkeventargs e) { //do here backgroundworkerresult.reportprogress(100); frmmessagebox frmmsgbox = new frmmessagebox(); frmmsgbox.showdialog(); }
even though showed frmmsgbox
dialog can still click form1
supposed not? how can fix this?
i created simple code sample can use understand how background worker works. copy code test form , add following controls
- label control - name 'lblstatus'
- progressbar control - should named progressbar1.
- add 2 buttons named 'btnstartasyncoperation' , 'btncancel' , link click events.
basically should display results in runworkercompleted event
public partial class form1 : form { backgroundworker backgroundworker; public form1() { initializecomponent(); backgroundworker = new backgroundworker {workerreportsprogress = true, workersupportscancellation = true}; backgroundworker.dowork += new doworkeventhandler(backgroundworker_dowork); backgroundworker.progresschanged += new progresschangedeventhandler(backgroundworker_progresschanged); backgroundworker.runworkercompleted += new runworkercompletedeventhandler(backgroundworker_runworkercompleted); } void backgroundworker_runworkercompleted(object sender, runworkercompletedeventargs e) { if (e.cancelled) lblstatus.text = "task cancelled."; else if (e.error != null) lblstatus.text = "error - " + e.error.message; else lblstatus.text = "task completed..."; btnstartasyncoperation.enabled = true; btncancel.enabled = false; } void backgroundworker_progresschanged(object sender, progresschangedeventargs e) { progressbar1.value = e.progresspercentage; lblstatus.text = "processing......" + progressbar1.value.tostring() + "%"; } void backgroundworker_dowork(object sender, doworkeventargs e) { (int = 0; < 100; i++) { system.threading.thread.sleep(100); backgroundworker.reportprogress(i); if (backgroundworker.cancellationpending) { e.cancel = true; backgroundworker.reportprogress(0); return; } } backgroundworker.reportprogress(100); } private void btnstartasyncoperation_click(object sender, eventargs e) { btnstartasyncoperation.enabled = false; btncancel.enabled = true; backgroundworker.runworkerasync(); } private void btncancel_click(object sender, eventargs e) { if (backgroundworker.isbusy) { backgroundworker.cancelasync(); } } }
edited per comment op
in dowork event, set string follows
e.result = "your string";
and in runworkercompleted event
string muresult = convert.tostring(e.result);
note: e.result object can set custom classes multiple properties in it.
Comments
Post a Comment