I know this has been covered before, but I'm not sure what I should do.
I want to display a progress bar during a file copy. All of the
examples I've seen accomplish this by reading the binary stream. This
seems like overkill. Doesn't it make more sense to get the size of the
file to be copied and then compare this to the size of the file that is
copying?
Of the samples of code I'm thinking of, they are about the same.
I've gathered that updating a progress bar should be done in a thread.
How would I accomplish this?
In my opinion, putting the copying process in a thread can be a lot of
work in some cases, but is usually the best idea. Here is some code
that copies a file using a binary stream and displays the progress on a
progress bar:
Sub CopyFile(source As Folderitem, dest As Folderitem)
Dim i As Integer
Dim sstream, dstream As BinaryStream
' don't forget to check for
' errors in source and dest
//open streams
sstream=source.OpenAsBinaryFile
dstream=dest.Child(source.Name).CreateBinaryFile
//set max of progress bar
Window1.ProgressBar1.Maximum=sstream.Length
//copy
While sstream.EOF=False
dstream.Write sstream.Read(1024)
ProgressBar1.Value=dstream.Length
Wend
End Sub
This is the bare minimum, and with no error checking. Source should be
a file, and dest should be a folder. To check for copying errors, you
can compare the lengths of the binary streams after the While loop.
They should be the same. I also believe that in RB 5.5 there is now a
LastErrorCode property for binary streams.
Tip: when trying to access controls such as a progress bar from a
thread that are on a window that has been instanced (you have created a
new copy of the window), you can put a property in the thread that
points to the target window (for example, MyDisplayWindow As Window1).
Then, if you have the display window create the thread, then you can
call Thread.MyDisplayWindow=Self . If the display window does not
create the thread, then you can use the variable that you created the
window with. The code would then be:
Dim w As Window1
Dim t As MyThread
w=new Window1
t=new MyThread
t.MyDisplayWindow=w
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://www.realsoftware.com/listarchives/lists.html>
|