How to Hook a Progress Bar to IO.Stream
A progress bar to IO stream indicates the progress of uploading or downloading files. An IO stream handles input and output functionality using streams. A stream is an abstraction which represents a device where input and output are conducted. Streams are related to a physical source like keyboard or an input console. C++ provides the standard iostream library including basic class templates, types and manipulators.
Instructions
-
-
1
Click “Start,” “All Programs” and “Visual Studio” to enter the Integrated Development Environment. Select programming language as Visual C++. Click “Form1.cs” to switch to the design view. Click “Toolbox” and “Button” to create a new Button named "button1."
-
2
Double-click the button1 and enter the following code to obtain information about input file:
using System;
using System.IO
System.IO.FileInfo fileInfo = new System.IO.FileInfo(FileTextBox.Text); -
-
3
Read from file "c:\\text1.txt" into the new FileStream variable "stream" using the following code:
using (System.IO.FileStream stream = new System.IO.FileStream("c:\\text1.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read))
The use of the ProgressChanged event reports the progress of upload. Obtain the upload progress using vairable "uploadStreamWithProgress."
{ using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream))
{ uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged; -
4
Double-click form1. Get the file upload progress via the following functions.
Define variable bytesRead: private long bytesRead;
Read the loaded bytes into variable "bytesRead" in function StreamWithProgress to
public StreamWithProgress(FileStream file)
{
this.file = file;
length = file.Length;
bytesRead = 0;
if (ProgressChanged != null) ProgressChanged(this, new ProgressChangedEventArgs(bytesRead, length));
}Calculate and return the progress of uploading in the GetProgress function:
public double GetProgress()
{
return ((double)bytesRead) / file.Length;
}
-
1
References
- Photo Credit Jupiterimages/Photos.com/Getty Images