Wpf Backgroundworker
程序员文章站
2023-04-04 14:43:21
<window x:class="wpfapp53.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:wpfapp53" mc:ignorable="d" title="mainwindow" height="450" width="800"> <grid> <grid.rowdefinitions> <rowdefinition height="*"/> <rowdefinition height="auto"/> <rowdefinition height="auto"/> <rowdefinition height="auto"/> </grid.rowdefinitions> <listbox grid.row="0" x:name="listbox1"/> <progressbar grid.row="1" x:name="progressbar1" height="20" margin="5"/> <stackpanel grid.row="2" orientation="horizontal" margin="5"> <textblock text="status: "/> <textblock x:name="statustextbox"/> </stackpanel> <grid grid.row="3" margin="10"> <grid.columndefinitions> <columndefinition width="*"/> <columndefinition width="*"/> </grid.columndefinitions> <button grid.column="0" x:name="startbtn" content="start" click="startbtn_click"/> <button grid.column="1" x:name="cancelbtn" content="cancel" click="cancelbtn_click"/> </grid> </grid> </window>
1 using system; 2 using system.collections.generic; 3 using system.componentmodel; 4 using system.linq; 5 using system.text; 6 using system.threading; 7 using system.threading.tasks; 8 using system.windows; 9 using system.windows.controls; 10 using system.windows.data; 11 using system.windows.documents; 12 using system.windows.input; 13 using system.windows.media; 14 using system.windows.media.imaging; 15 using system.windows.navigation; 16 using system.windows.shapes; 17 18 namespace wpfapp53 19 { 20 /// <summary> 21 /// interaction logic for mainwindow.xaml 22 /// </summary> 23 public partial class mainwindow : window 24 { 25 backgroundworker bgworker; 26 public mainwindow() 27 { 28 initializecomponent(); 29 cancelbtn.isenabled = false; 30 } 31 32 private void startbtn_click(object sender, routedeventargs e) 33 { 34 listbox1.items.clear(); 35 startbtn.isenabled = false; 36 cancelbtn.isenabled = true; 37 bgworker = new backgroundworker(); 38 bgworker.dowork += bgworker_dowork; 39 bgworker.progresschanged += bgworker_progresschanged; 40 bgworker.runworkercompleted += bgworker_runworkercompleted; 41 bgworker.workerreportsprogress = true; 42 bgworker.workersupportscancellation = true; 43 44 int maxitems = 50; 45 progressbar1.minimum = 1; 46 progressbar1.maximum = 100; 47 48 statustextbox.text = "starting..."; 49 bgworker.runworkerasync(maxitems); 50 } 51 52 private void bgworker_runworkercompleted(object sender, runworkercompletedeventargs e) 53 { 54 if(e.cancelled) 55 { 56 statustextbox.text = "cancelled"; 57 } 58 else 59 { 60 statustextbox.text = "completed"; 61 } 62 statustextbox.isenabled = true; 63 cancelbtn.isenabled = false; 64 } 65 66 private void bgworker_progresschanged(object sender, progresschangedeventargs e) 67 { 68 double percent = (e.progresspercentage * 100) / 50; 69 progressbar1.value = math.round(percent, 0); 70 listbox1.items.add(new listboxitem { content = e.progresspercentage + " item added" }); 71 statustextbox.text = math.round(percent, 0) + "% percent completed"; 72 } 73 74 private void bgworker_dowork(object sender, doworkeventargs e) 75 { 76 int? maxitems = e.argument as int?; 77 for(int i=1;i<=maxitems.getvalueordefault();i++) 78 { 79 if(bgworker.cancellationpending) 80 { 81 e.cancel = true; 82 break; 83 } 84 thread.sleep(200); 85 bgworker.reportprogress(i); 86 } 87 } 88 89 private void cancelbtn_click(object sender, routedeventargs e) 90 { 91 bgworker.cancelasync(); 92 startbtn.isenabled = true; 93 } 94 } 95 }