shawl01
asked on
Update form progress bar from within class
I am using the Microsoft FTP class to carry out some ftp functions. when I use the download function of the class I would like to be able to update a progress bar on the current form. As I will call the class from many different forms I am not sure how to handle this as when the download function is called the program does not hand back control to the form until the download has completed.
I have included a copy of the relevant MS download subprocedure that is called from my windows form
500 points for a working solution.
Public Sub DownloadFile(ByVal sFileName As String, _
ByVal sLocalFileName As String, _
ByVal bResume As Boolean)
Dim st As Stream
Dim output As FileStream
Dim cSocket As Socket
Dim offset, npos As Long
If (Not (m_bLoggedIn)) Then
Login()
End If
SetBinaryMode(True)
If (sLocalFileName.Equals("") ) Then
sLocalFileName = sFileName
End If
If (Not (File.Exists(sLocalFileNam e))) Then
st = File.Create(sLocalFileName )
st.Close()
End If
output = New FileStream(sLocalFileName, FileMode.Open)
cSocket = CreateDataSocket()
offset = 0
If (bResume) Then
offset = output.Length
If (offset > 0) Then
'Send an FTP command to restart.
SendCommand("REST " & offset)
If (m_iRetValue <> 350) Then
offset = 0
End If
End If
If (offset > 0) Then
npos = output.Seek(offset, SeekOrigin.Begin)
End If
End If
'Send an FTP command to retrieve a file.
SendCommand("RETR " & sFileName)
If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
output.Close()
MessageString = m_sReply
Throw New IOException(m_sReply.Subst ring(4))
End If
Do While (True)
m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)
m_iBytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
output.Write(m_aBuffer, 0, m_iBytes)
If (m_iBytes <= 0) Then
Exit Do
End If
Loop
output.Close()
If (cSocket.Connected) Then
cSocket.Close()
End If
ReadReply()
If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Subst ring(4))
End If
End Sub
I have included a copy of the relevant MS download subprocedure that is called from my windows form
500 points for a working solution.
Public Sub DownloadFile(ByVal sFileName As String, _
ByVal sLocalFileName As String, _
ByVal bResume As Boolean)
Dim st As Stream
Dim output As FileStream
Dim cSocket As Socket
Dim offset, npos As Long
If (Not (m_bLoggedIn)) Then
Login()
End If
SetBinaryMode(True)
If (sLocalFileName.Equals("")
sLocalFileName = sFileName
End If
If (Not (File.Exists(sLocalFileNam
st = File.Create(sLocalFileName
st.Close()
End If
output = New FileStream(sLocalFileName,
cSocket = CreateDataSocket()
offset = 0
If (bResume) Then
offset = output.Length
If (offset > 0) Then
'Send an FTP command to restart.
SendCommand("REST " & offset)
If (m_iRetValue <> 350) Then
offset = 0
End If
End If
If (offset > 0) Then
npos = output.Seek(offset, SeekOrigin.Begin)
End If
End If
'Send an FTP command to retrieve a file.
SendCommand("RETR " & sFileName)
If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
output.Close()
MessageString = m_sReply
Throw New IOException(m_sReply.Subst
End If
Do While (True)
m_aBuffer.Clear(m_aBuffer,
m_iBytes = cSocket.Receive(m_aBuffer,
output.Write(m_aBuffer, 0, m_iBytes)
If (m_iBytes <= 0) Then
Exit Do
End If
Loop
output.Close()
If (cSocket.Connected) Then
cSocket.Close()
End If
ReadReply()
If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Subst
End If
End Sub
ASKER
I would prefer to use my own form as it has other info on it as well and is already loaded and displaying on screen at the point where the ftp download is called. I want to achieve this within vb.net
Is their not some way of referencing a calling forms controls to update them and then update the on-screen display ?
Is their not some way of referencing a calling forms controls to update them and then update the on-screen display ?
so add a progress bar ro your form and make a public sub
Public Sub UpdateProgressBar()
ProgressBar1.value = ProgressBar1.value + 1
end sub
and call that function from within your class
Public Sub UpdateProgressBar()
ProgressBar1.value = ProgressBar1.value + 1
end sub
and call that function from within your class
If you have the code for the class (or can inherit from it) you could add an Event to the class that fires at intervals during the transfer, and then subscribe ot the event from your form to handle updating the progress bar.
ASKER
carl tawn
Can you give any more details on this and maybe an example
I am not sure what you mean when you say subscribe to an event
Thanks
Can you give any more details on this and maybe an example
I am not sure what you mean when you say subscribe to an event
Thanks
Say, for example, that your FTP class had an event called Transferring that fired every so often during the transfer and in your form you created an object like:
Public Sub Form_Load()
Dim obj As New YourFTPClass()
AddHandler obj.Transferring, AddressOf UpdateProgressBar
End Sub
Public Sub UpdateProgressBar()
'// This sub will be called everytime the Transferring event is fired by the YourFTPClass object
End Sub
Public Sub Form_Load()
Dim obj As New YourFTPClass()
AddHandler obj.Transferring, AddressOf UpdateProgressBar
End Sub
Public Sub UpdateProgressBar()
'// This sub will be called everytime the Transferring event is fired by the YourFTPClass object
End Sub
ASKER
Can you tell me how to define the event? I thought I knew but after a bit of reading I am now more confused
Sorry to be a pain
Thanks
Sorry to be a pain
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
so add a progress bar ro your form and make a public sub
Public Sub UpdateProgressBar()
ProgressBar1.value = ProgressBar1.value + 1
end sub
and call that function from within your class
Public Sub UpdateProgressBar()
ProgressBar1.value = ProgressBar1.value + 1
end sub
and call that function from within your class
The problem with that method is that the FTP clas then becomes tied to a specific form, or at least a form with an UpdateProgressBar method. By using events instead you allow the FTP class to notify consumers when something happens, and it is up to the Form (or whatever object is using the FTP class) whether they want to know about it or not.
he just wants to update a form. Its simple
ASKER
carl tawn
Many thanks your solution worked, I managed to define two events 1) Initialise the progress bar 2) Increment the progress bar passing through the additional bytes downloaded. I can also use this from different forms without any additional coding.
Mika613
carl tawn is correct in what he is saying about tying to a specific form, if you check my initial question you will see that I said I would be calling it from different forms.
Many thanks to both for your suggestions.
I will award the points to carl tawn.
Many thanks your solution worked, I managed to define two events 1) Initialise the progress bar 2) Increment the progress bar passing through the additional bytes downloaded. I can also use this from different forms without any additional coding.
Mika613
carl tawn is correct in what he is saying about tying to a specific form, if you check my initial question you will see that I said I would be calling it from different forms.
Many thanks to both for your suggestions.
I will award the points to carl tawn.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/progbar/progbar.asp
you can do that