-1

Have some code which uploads files to FTP and it takes some time before it ends and reaches next line of code. In the meantime I would like to show a progress bar and freeze window form until it's done. How to achieve that?

WinScp.PutFile(File, destFP, True)           '<---this line taking some time

    'Progress bar here till it reach next line...

    If Not lsbxPicPaths.Items.Contains(destFP) Then    
        lsbxPicPaths.Items.Add(destFP)
    Else
    ...

For further discussion

Before open.Session i added:

AddHandler session.FileTransferProgress, AddressOf SessionFileTransferProgress

and below staff was created:

Function GetFile(source As String, destination As String, Optional removeSource As Boolean = False)
            Dim result As Boolean = True
            Try



                session.GetFiles(source, destination, removeSource).Check()




            Catch ex As Exception
                result = False
                'Logger.LogIt(Alert.Write(MsgType.ERROR), Eng.Write(EngType.COLLEC), Datasource.ToString & " | " & Me.reportName & " | " & ex.ToString, LogPath, isDebug)
            End Try
            Return result
        End Function

        Private Shared Sub SessionFileTransferProgress(sender As Object, e As FileTransferProgressEventArgs)
            'Print transfer progress
            _lastProgress = e.FileProgress

        End Sub

        Public Shared _lastProgress As Integer

    then calling like this:

      Me.Cursor = Cursors.WaitCursor
                                WinScp.GetFile(myremotePicturePath, ladujZdjeciaPath, True)


          form.ProgressBar1.Value = 0
                                ProgressBar1.Show()

                                Do
                                    ProgressBar1.Value = WinScpOperation._lastProgress
                                    ProgressBar1.Refresh()
                                Loop Until ProgressBar1.Value = 1



                                Me.Cursor = Cursors.Default

but then besides i want to show custom form when progress bar on it and which will show during getting file and show progress then dissaper and unlock parent form. I don;t know how to pass value to my dynamic form's progress bar. I tried to start like this to change my current code:

Me.Cursor = Cursors.WaitCursor
                            WinScp.GetFile(myremotePicturePath, ladujZdjeciaPath, True)


                            Dim form As New Form
                            Dim pb As New ProgressBar
                            pb.Minimum = 0
                            pb.Maximum = 1

                            form.ShowDialog()




'pass value to progress bar
                            form.ProgressBar1.Value = 0




                            ' ProgressBar1.Hide()
                            'ProgressBar1.Value = 0

form.Close()



                            Me.Cursor = Cursors.Default

for further discussion nr.2

New form:

Public Class FrmProgressBarWinscp

    Property value As Integer

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        value = 0
        ProgressBar1.Value = 0
        ProgressBar1.Show()

    End Sub

    Sub Run()


        Do
            ProgressBar1.Value = value
            ProgressBar1.Refresh()
        Loop Until value = 1
        MsgBox("Done")
        'ProgressBar1.Hide()
        'ProgressBar1.Value = 0
    End Sub


End Class

and on oryginal form:

WinScp.GetFile(myremotePicturePath, ladujZdjeciaPath, True)

 Dim pro As New FrmProgressBarWinscp()
                            pro.ShowDialog()

                            Do
                                pro.value = WinScpOperation._lastProgress
                                pro.Run()
                            Loop Until WinScpOperation._lastProgress

                            Me.Cursor = Cursors.Default
Arie
  • 3,041
  • 7
  • 32
  • 63
  • Related question: [Showing WinSCP .NET assembly transfer progress on WinForm's progress bar](http://stackoverflow.com/q/33012517/850848) – Martin Prikryl Oct 08 '15 at 11:20

2 Answers2

0

I've never used this before, but after a bit of looking around on the WINSCP website, this might work. You might need to look into it more yourself to get it working.

If the file transfer runs in its own thread and doesn't freeze the ui, try this

Add a progress bar control to your form Set The minimum property to 0 Set the maximum property to 1 set the visible property to false at the point you want to display the progress bar, insert code something like

progressbar1.value=0
progressbar1.show
Do
    progressbar1.value=winscp.FileProgressEventArgs.FileProgress
    progressbar1.refresh
until progressbar1.value=1
progressbar1.hide
progressbar.value=0

You might need to choose a different condition for the Do/Loop termination e.g. if WINSCP changed a flag to True when the file is completed and you'll need to terminate the loop if the transfer fails.

David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • it's working however i would like to show progress bar in separate window (form) when file is downloading therefore i created dynamic form, but have no idea how to pass the value to it and when it's finished dynamic form with its progress bar will dissaper and oryginal form will be operational again. And if possible to show progress from 0 to 100 rather by 0 to 1. Can you help? I put my new code into main topic. – Arie Oct 07 '15 at 11:24
  • The same as in your previous answer: 1) The WinSCP [`Session.GetFiles`](https://winscp.net/eng/docs/library_session_getfiles) is blocking, so this cannot work. 2) There's no `winscp` field anywhere on the original code. 3) [`FileTransferProgressEventArgs`](https://winscp.net/eng/docs/library_filetransferprogresseventargs) (not `FileProgressEventArgs`) is a class, so you cannot access its `FileProgress` this way. – Martin Prikryl Oct 08 '15 at 11:24
  • 1
    And you are not supposed to post two nearly identical answers. If you want to improve the previous answer, edit it, do not post a new one. – Martin Prikryl Oct 08 '15 at 11:24
  • Martin. Rather than pick what you perceive as error in someone else's code, and criticising other people's attempts to help. Perhaps you should take time to learn tact and diplomacy. This would help the community grow rather than put off new members from trying to help. Yes the answers are similar, but I thought it would be clearer to post two separate answers rather than have a large lump of code. – David Wilson Oct 08 '15 at 13:01
  • 1) Your are answers are so wrong, that they deserve criticism. But my point was to to criticize, but to show to OR and mainly to random readers that this solution is not to be followed. Please next time make more research to avoid posting a "solution" that only confuses the OP and others. 2) It's not better to post two separate answers, unless they present two completely different approaches. 3) Use `@username` to reply someone. I do not get notified otherwise. I've found your response by a chance only 4) Welcome to Stack Overflow! :) – Martin Prikryl Oct 08 '15 at 14:45
  • Heh. Thanks - tho i did say that i had never used winscp before, I presume then that answers should only be submitted if they are correct and complete then, rather than trying to point someone in the right general direction? :) – David Wilson Oct 09 '15 at 23:23
0

Rather than use dim to create the form, I would add the form using visual studio's add new item - which is accessible by right clicking on your Project name(not solution name) on the right hand side. A context menu pops up. Choose "Add" then click "New Item" and choose the appropriate form that is the same type as your project.

Add the progressBar to the form - and you can leave the name as progressBar1 if you want, but if you change is you need to change the references to it in the code below

At the very top of the code for the new form add the same imports line that you used to import the winscp stuff.

Put this block of code (similar to your code in your updated question) into the new form. I've edited the code a bit so that it should work in the dynamic form.

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
    Me.Cursor = Cursors.WaitCursor
    WinScp.GetFile(myremotePicturePath, ladujZdjeciaPath, True)
    progressBar1.Minimum = 0
    progressBar1.Maximum = 1
    progressBar1.Value = 0
    Do
        progressBar1.value=winscp.FileProgressEventArgs.FileProgress
        progressbar1.refresh
    until progressbar1.value=1
    Me.Cursor = Cursors.Default
    form.Close()
End Sub

e.g Form1.myremotePicturePath and Form1.ladujZdjeciaPath.

Also where you originally define the function and those two variables in your original Form, you'll need to change their from Private to Public so that the new form can see them

I think that should do it. If you have any problems let me know, but I wont be home till lateish in the next few days, so I may not reply till late in the evening - a bit like tonight :))

David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • this is working but new form show up not straightaway but main form is freezing and new form is showing up later. Thats the current issue, to avoid long topics i made new one can you take a look?: http://stackoverflow.com/questions/33012517/forms-progress-bar-doesnt-show-straightaway-winscp – Arie Oct 08 '15 at 10:01
  • 1) The WinSCP [`Session.GetFiles`](https://winscp.net/eng/docs/library_session_getfiles) is blocking, so this cannot work. 2) There's no `winscp` field anywhere on the original code. 3) [`FileTransferProgressEventArgs`](https://winscp.net/eng/docs/library_filetransferprogresseventargs) (not `FileProgressEventArgs`) is a class, so you cannot access its `FileProgress` this way. – Martin Prikryl Oct 08 '15 at 11:22