Display progress bar for wpf file copy

Source: Internet
Author: User
There are also some introductions, first look at the running effect
The xaml is as follows:
Xaml code
   
<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">
& Lt; grid height = "186" & gt;
<Grid. columndefinitions>
<Columndefinition width = "132 *" type = "codeph" text = "/codeph"/>
<Columndefinition width = "236 *" type = "codeph" text = "/codeph"/>
<Columndefinition width = "147 *" type = "codeph" text = "/codeph"/>
</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 = "Current Time" 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 = "source file location"/>
<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 = "target file location" margin = "35 10 0 10"/>
<Button content = "... "grid. column = "2" grid. row = "1" name = "button1" margin = "10, 10, 66,10" click = "button#click"/>
<Button content = "... "grid. column = "2" grid. row = "2" name = "button2" margin = "10, 10, 66,10" height = "23" click = "button2_click"/>
<Button content = "start time thread" grid. column = "2" name = "button3" margin = "10, 10, 35, 10" height = "23" click = "button3_click"/>
<Button content = "start file copy thread" grid. column = "2" grid. row = "3" height = "29" horizontalalignment = "left" name = "button4" verticalignment = "top" width = "119" click = "button4_click"/>
<Textblock grid. row = "3" name = "copyflag" text = "start copying"/>
<Textblock name = "displaycopyinfo" text = "file copy in progress" grid. row = "3" grid. column = "1"/>
<Progressbar grid. column = "1" grid. row = "4" margin = "0 2" height = "8" name = "copyprogress"/>
</Grid>
</Window>
Backend cs

 

Background cs content
   
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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.