• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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(sLocalFileName))) 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.Substring(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.Substring(4))
        End If

    End Sub
0
shawl01
Asked:
shawl01
  • 4
  • 4
  • 4
1 Solution
 
shawl01Author Commented:
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 ?
0
 
Mikal613Commented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
 
shawl01Author Commented:
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
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
0
 
shawl01Author Commented:
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
0
 
Carl TawnSystems and Integration DeveloperCommented:
In your class you would define an event called Transferring with something like:

    Public Event Transferring()     '// This is the event, you can add arguments to return data to the event handler if you needed to.

You would then raise the event inside your Sub (probably as part of your Do loop), something like:

        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)

            RaiseEvent Transferring()        '// Raises the event i.e. calls any methods that have subscribed

            If (m_iBytes <= 0) Then
                Exit Do
            End If
        Loop
0
 
Mikal613Commented:
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
0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
 
Mikal613Commented:
he just wants to update a form. Its simple
0
 
shawl01Author Commented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now