【www.gdgbn.com--WPF】

也有一些介绍,先看运行效果 xaml如下: xaml代码
        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">



















后台cs

 

jquery1301893080328="2" alt="" src="http://filesimg.111cn.net/2010/03/2011040405005659.gif" />后台cs内容
1 namespace wpfthreadtest
2 {
3 ///
4 /// mainwindow.xaml 的交互逻辑
5 ///

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 ///
104 /// 复制文件的委托方法
105 ///

106 /// 复制文件的信息
107 private void copyfile(object obj)
108 {
109 copyfileinfo c = obj as copyfileinfo;
110 copyfile(c.sourcepath, c.destpath);
111 }
112 ///
113 /// 复制文件
114 ///

115 ///
116 ///
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(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 }

本文来源:http://www.gdgbn.com/asp/29715/