先看运行效果
xaml如下:
xaml代码
<window x:class="wpfthreadtest.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" closed="window_closed" loaded="window_loaded" height="209" width="537" resizemode="noresize"> <grid height="186"> <grid.columndefinitions> <columndefinition width="132*" /> <columndefinition width="236*" /> <columndefinition width="147*" /> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="auto" /> <rowdefinition height="auto" /> <rowdefinition height="auto" /> <rowdefinition height="auto" /> </grid.rowdefinitions> <textblock name="textblock1" text="当前时间" margin="35 10" height="auto" width="auto" /> <textbox grid.column="1" name="displaytimebythread" height="auto" margin="0 10" /> <textblock grid.row="1" name="textblock2" margin="35 10 0 10 " text="源文件位置" /> <textbox grid.column="1" grid.row="1" name="srcfile" margin="0 10" /> <textbox grid.column="1" grid.row="2" name="savefilepath" margin="0 10" /> <textblock grid.row="2" name="textblock3" text="目标文件位置" margin="35 10 0 10" /> <button content="..." grid.column="2" grid.row="1" name="button1" margin="10,10,66,10" click="button1_click" /> <button content="..." grid.column="2" grid.row="2" name="button2" margin="10,10,66,10" height="23" click="button2_click"/> <button content="开始时间线程" grid.column="2" name="button3" margin="10,10,35,10" height="23" click="button3_click" /> <button content="开始文件copy线程" grid.column="2" grid.row="3" height="29" horizontalalignment="left" name="button4" verticalalignment="top" width="119" click="button4_click" /> <textblock grid.row="3" name="copyflag" text="开始复制" /> <textblock name="displaycopyinfo" text="文件copy进行中" grid.row="3" grid.column="1" /> <progressbar grid.column="1" grid.row="4" margin="0 2" height="8" name="copyprogress" /> </grid> </window>
后台cs
后台cs内容
1 namespace wpfthreadtest 2 { 3 /// <summary> 4 /// mainwindow.xaml 的交互逻辑 5 /// </summary> 6 public partial class mainwindow : window 7 { 8 thread timethread; 9 thread copythread; 10 public mainwindow() 11 { 12 initializecomponent(); 13 this.displaytimebythread.text = datetime.now.tolocaltime().tostring("yyyy年mm月dd日 hh:mm:ss"); ; 14 timethread = new thread(new threadstart(dispatcherthread)); 15 } 16 private void button3_click(object sender, routedeventargs e) 17 { 18 timethread.start(); 19 } 20 public void dispatcherthread() 21 { 22 //可以通过循环条件来控制ui的更新 23 while (true) 24 { 25 ///线程优先级,最长超时时间,方法委托(无参方法) 26 displaytimebythread.dispatcher.begininvoke( 27 dispatcherpriority.normal, new action(updatetime)); 28 thread.sleep(1000); 29 } 30 } 31 32 33 private void updatetime() 34 { 35 this.displaytimebythread.text = datetime.now.tolocaltime().tostring("yyyy年mm月dd日 hh:mm:ss"); 36 } 37 38 private void window_closed(object sender, eventargs e) 39 { 40 ///关闭所有启动的线程 41 timethread.abort(); 42 copythread.abort(); 43 application.current.shutdown(); 44 } 45 46 private void button1_click(object sender, routedeventargs e) 47 { 48 ///设定要复制的文件全路径 49 openfiledialog openfile = new openfiledialog(); 50 openfile.addextension = true; 51 openfile.checkpathexists = true; 52 openfile.filter = "*.rar|*.rar|all files|*.*"; 53 openfile.filterindex = 0; 54 openfile.multiselect = false; 55 bool? f=openfile.showdialog(); 56 if (f!=null && f.value) 57 { 58 this.srcfile.text = openfile.filename; 59 } 60 } 61 62 private void button2_click(object sender, routedeventargs e) 63 { 64 ///设定目标文件全路径 65 savefiledialog savefile = new savefiledialog(); 66 savefile.addextension = true; 67 savefile.filter = "*.rar|*.rar|all files|*.*"; 68 savefile.filterindex = 0; 69 70 bool? f= savefile.showdialog(); 71 if (f != null && f.value) 72 { 73 this.savefilepath.text = savefile.filename; 74 } 75 } 76 77 private void button4_click(object sender, routedeventargs e) 78 { 79 string filename=this.srcfile.text.trim(); 80 string destpath=this.savefilepath.text.trim(); 81 if(!file.exists(filename)) 82 { 83 messagebox.show("源文件不存在"); 84 return; 85 } 86 87 ///copy file and nodify ui that rate of progress of file copy 88 this.copyflag.text = "开始复制。。。"; 89 90 //设置进度条最大值,这句代码写的有点郁闷 91 this.copyprogress.maximum = (new fileinfo(filename)).length; 92 93 //保存复制文件信息,以进行传递 94 copyfileinfo c = new copyfileinfo() { sourcepath = filename, destpath = destpath }; 95 //线程异步调用复制文件 96 copythread = new thread(new parameterizedthreadstart(copyfile)); 97 copythread.start(c); 98 99 this.copyflag.text = "复制完成。。。"; 100 101 102 } 103 /// <summary> 104 /// 复制文件的委托方法 105 /// </summary> 106 /// <param name="obj">复制文件的信息</param> 107 private void copyfile(object obj) 108 { 109 copyfileinfo c = obj as copyfileinfo; 110 copyfile(c.sourcepath, c.destpath); 111 } 112 /// <summary> 113 /// 复制文件 114 /// </summary> 115 /// <param name="sourcepath"></param> 116 /// <param name="destpath"></param> 117 private void copyfile( string sourcepath,string destpath) 118 { 119 fileinfo f = new fileinfo(sourcepath); 120 filestream fsr = f.openread(); 121 filestream fsw = file.create(destpath); 122 long filelength = f.length; 123 byte[] buffer = new byte[1024]; 124 int n = 0; 125 126 while (true) 127 { 128 ///设定线程优先级 129 ///异步调用updatecopyprogress方法 130 ///并传递2个long类型参数filelength 与 fsr.position 131 this.displaycopyinfo.dispatcher.begininvoke(dispatcherpriority.systemidle, 132 new action<long, long>(updatecopyprogress), filelength, fsr.position); 133 134 //读写文件 135 n=fsr.read(buffer, 0, 1024); 136 if (n==0) 137 { 138 break; 139 } 140 fsw.write(buffer, 0, n); 141 fsw.flush(); 142 thread.sleep(1); 143 } 144 fsr.close(); 145 fsw.close(); 146 } 147 148 private void updatecopyprogress(long filelength,long currentlength) 149 { 150 this.displaycopyinfo.text = string.format("总大小:{0},已复制:{1}", filelength, currentlength); 151 //刷新进度条 152 this.copyprogress.value = currentlength; 153 } 154 155 private void window_loaded(object sender, routedeventargs e) 156 { 157 158 } 159 160 } 161 public class copyfileinfo 162 { 163 public string sourcepath { get; set; } 164 public string destpath { get; set; } 165 } 166 }