We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Update form progress bar from within class

shawl01
shawl01 asked
on
Medium Priority
304 Views
Last Modified: 2008-02-01
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
Comment
Watch Question

CERTIFIED EXPERT

Commented:

Author

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 ?
CERTIFIED EXPERT

Commented:
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
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
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.

Author

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
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
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

Author

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
Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT

Commented:
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
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT

Commented:
he just wants to update a form. Its simple

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.