1 namespace wpfthreadtest
2 {
3 /// <summary>
4 // interaction logic of 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 // you can control ui updates through cyclic conditions.
23 while (true)
24 {
25 // thread priority, maximum timeout, method delegate (no parameter)
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 // close all started threads
41 timethread. abort ();
42 copythread. abort ();
43 application. current. shutdown ();
44}
45
46 private void button#click (object sender, routedeventargs e)
47 {
48 /// set the full path of the file to be copied
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 // set the full path of the target file
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 ("the source file does not exist ");
84 return;
85}
86
87 // copy file and nodify ui that rate of SS of file copy
88 this. copyflag. text = "start copying... ";
89
90 // set the maximum value of the progress bar. This code is a bit depressing.
91 this. copyprogress. maximum = (new fileinfo (filename). length;
92
93 // Save the copied file information for transmission
94 copyfileinfo c = new copyfileinfo () {sourcepath = filename, destpath = destpath };
95 // the thread asynchronously calls the copy object
96 copythread = new thread (new parameterizedthreadstart (copyfile ));
97 copythread. start (c );
98
99 this. copyflag. text = "copied... ";
100
101
102}
103 /// <summary>
104 // copy the file delegate method
105 /// </summary>
106 /// <param name = "obj"> Copy file information </param>
107 private void copyfile (object obj)
108 {
109 copyfileinfo c = obj as copyfileinfo;
110 copyfile (c. sourcepath, c. destpath );
111}
112 /// <summary>
113 // copy the file
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 // Set the thread priority
129 // call the updatecopyprogress method asynchronously
130 // and pass two long-type parameters filelength and fsr. position
131 this. displaycopyinfo. dispatcher. begininvoke (dispatcherpriority. systemidle,
132 new action <long, long> (updatecopyprogress), filelength, fsr. position );
133
134 // read and write files
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 ("total size: {0}, copied: {1}", filelength, currentlength );
151 // refresh the progress bar
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}