c# - How to pause for loop when pause button is clicked? -
when run program , try click pause button, nothing happens. not sure how can work exactly. have bool variable called pause , pause set false. once pause button clicked should set variable true. loop checks , should display message user. appreciated!
namespace practice2 { public partial class form1 : form { photocopier printer = new photocopier(500, 2500); bool pause = false; public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { } private void btnstart_click(object sender, eventargs e) { if (checktext(txtnumbercopies.text) == true) { int numbercopies = convert.toint32(txtnumbercopies.text); int toner = convert.toint32(lbltoneramount.text); int papercapacity = convert.toint32(lblpaperamount.text); if (toner <= 625 && papercapacity <= 125) { txtmessage.text = "printer low on toner , paper!"; } else if (toner <= 625){ txtmessage.text = "printer toner low!"; } else if (papercapacity <= 125) { txtmessage.text = "printer paper low!"; } else { txtmessage.text = "printing..."; txtmessage.refresh(); (int = numbercopies; != 0; i--) { int paper = convert.toint32(lblpaperamount.text); paper--; if (paper == 480 || paper == 380 || paper == 400 || paper == 200) { messagebox.show("there paper jam! please remove jam , hit ok button continue!", "important message", messageboxbuttons.ok, messageboxicon.exclamation); } if (pause == true) { messagebox.show("press ok button when ready continue", "important message", messageboxbuttons.ok, messageboxicon.exclamation); } lblpaperamount.text = convert.tostring(convert.toint32(lblpaperamount.text) - 1); lbltoneramount.text = convert.tostring(convert.toint32(lbltoneramount.text) - 1); thread.sleep(1000); } txtmessage.text = "job completed!"; } } } private void btnaddpaper_click(object sender, eventargs e) { int paperamount = convert.toint32(lblpaperamount.text); if (checktext(txtaddpaper.text) == true && paperamount <= 500) { lblpaperamount.text = convert.tostring(paperamount + convert.toint32(txtaddpaper.text)); } else { txtmessage.text = "printer paper @ capacity!"; } } private bool checktext(string textbox) { if (textbox.equals("") || textbox == null) { txtmessage.text = "please enter value in text box!"; return false; } return true; } private void btnreplacetoner_click(object sender, eventargs e) { lbltoneramount.text = convert.tostring(printer.toner); } private void btnpausecancel_click(object sender, eventargs e) { pause = true; } } }
the problem you're doing work on ui thread, ui thread busy , can't process messages (e.g. button click). need work on worker thread instead, using backgroundworker
or task.run
instance.
Comments
Post a Comment