This repository has been archived by the owner on Aug 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgressStream.cs
200 lines (172 loc) · 3.26 KB
/
ProgressStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.IO;
using System.Windows.Forms;
namespace WinMD5
{
/// <summary>
/// Summary description for ProgressStream.
/// </summary>
public class ProgressStream : Stream
{
public delegate void ProgressUpdateEvent(long v, long m);
Stream fs;
long readcount=0;
long readnotifycount;
long fileLength=0;
static long fileLengthDivider=1024;
static long numNotifications=200;
ProgressBar progressBar;
bool abort=false;
public ProgressStream(Stream fs, ProgressBar progressBar)
{
this.fs=fs;
// this.readnotifycount=readnotifycount;
this.progressBar=progressBar;
this.fileLength=fs.Length;
this.readnotifycount=fileLength/fileLengthDivider/numNotifications;
progressBar.Invoke(new EventHandler(SetMaximumPosition));
}
protected void SetMaximumPosition(object o, EventArgs e)
{
lock(this)
{
System.Diagnostics.Debug.Assert(progressBar.InvokeRequired==false);
progressBar.Maximum=(int) (1+fileLength/fileLengthDivider);
}
}
public override bool CanRead
{
get
{
lock(this)
{
return fs.CanRead;
}
}
}
public override bool CanSeek
{
get
{
lock(this)
{
return fs.CanSeek;
}
}
}
public override bool CanWrite
{
get
{
lock(this)
{
return fs.CanWrite;
}
}
}
public override long Length
{
get
{
lock(this)
{
return fileLength;
}
}
}
public override long Position
{
get
{
lock(this)
{
return fs.Position;
}
}
set
{
lock(this)
{
fs.Position=value;
}
}
}
public override void Flush()
{
lock(this)
{
fs.Flush();
}
}
public override int Read(byte[] buffer, int offset, int count)
{
lock(this)
{
if (abort)
throw new IOException("aborted");
int len=fs.Read(buffer,offset,count);
readcount+=len;
if (readcount>readnotifycount)
{
readcount=0;
// readcount-=readnotifycount;
// if (ProgressUpdate!=null)
// ProgressUpdate(this.Position, this.Length);
if (progressBar!=null)
{
progressBar.BeginInvoke(new ProgressUpdateEvent(ProgressBarUpdateProc), new object[] {0,0});
}
}
return len;
}
}
void ProgressBarUpdateProc(long v, long m)
{
lock(this)
{
System.Diagnostics.Debug.Assert(progressBar.InvokeRequired==false);
long pos=this.Position;
if (pos==fileLength)
progressBar.Value=progressBar.Maximum;
else
progressBar.Value=(int) (pos/fileLengthDivider);
}
}
public override long Seek(long offset, SeekOrigin origin)
{
lock(this)
{
return fs.Seek(offset, origin);
}
}
public override void SetLength(long value)
{
lock(this)
{
fs.SetLength(value);
}
}
public override void Write(byte[] buffer, int offset, int count)
{
lock(this)
{
fs.Write(buffer, offset, count);
}
}
public override void Close()
{
lock(this)
{
fs.Close();
base.Close();
}
}
public void AbortRead()
{
lock(this)
{
abort=true;
}
}
}
}