我从ssh下载了一个文件,但完成下载需要50秒

时间:2014-04-01 10:43:34

标签: vb.net winforms ssh

我正在使用vb.net下载文件并使用Tamir.SharpSsh,除了速度慢,下载3.5 kb文件大约需要50秒。我的问题是如何最好将等待功能放入2分钟,以确保下载文件。

Public Function DownloadPricat() As Boolean
    Dim retVal As Boolean
    Dim PRICAT_CSV As String
    Dim sfilename As String = ""
    Dim ifilename As String

    utils = New ThreeSoftware.Configuration.Utilities.utilConfigurationLoader("CONFIGURATION FILES\GEMINI RELATED\SkechersImport.ini")

    Hostname = utils.GetIniSetting("SSH SECTION", "SSH_HOST", "")
    username = utils.GetIniSetting("SSH SECTION", "SSH_USERNAME", "")
    passsword = utils.GetIniSetting("SSH SECTION", "SSH_PASSWORD", "")
    port = utils.GetIniSetting("SSH SECTION", "SSH_PORT", "")
    HomeDirectoy = utils.GetIniSetting("SSH SECTION", "SSH_REMOTE_DIRECTORY", "")


    transfer = New wcSFtp(Hostname, Integer.Parse(port), username, passsword)

    PRICAT_CSV = utils.GetIniSetting("PATHS SECTION", "PRICAT_CSV", "")
    sfilename = utils.GetIniSetting("PATHS SECTION", "PRICAT_FILENAME", "")

    ifilename = PRICAT_CSV & "\" & sfilename
    If transfer.getFile(HomeDirectoy & "Pricat.edi", ifilename) = True Then
        MsgBox("Download Complete", vbInformation, "Import")
        retVal = True
    Else

        retVal = False
    End If

End Function

Get File就是这个

Public Function getFile(ByVal remotePath As String, ByVal localFile As String) As Boolean
    Try
        transfer = New Sftp(Me._hostname, Me._username, Me._password)
        transfer.Connect(Me._port)
        transfer.Get(remotePath, localFile)
        transfer.Close()
        Return True
    Catch ex As Exception
        Debug.Print("Error downloading file: " & ex.ToString)
        Return False
    End Try
End Function

1 个答案:

答案 0 :(得分:0)

将整个下载放入后台工作人员DoWork()函数中,并将布尔结果添加为eventargs变量的结果变量。 然后处理后台工作程序的RunWorkerCompleted()事件,并执行从那里下载后要执行的任何任务。这样就可以确保下载实际完成。

Public Class Form1
Private WithEvents LazyBGW As New System.ComponentModel.BackgroundWorker

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Initiate the backgroundworker. It runs in another thread.
    LazyBGW.RunWorkerAsync()
End Sub

Private Sub LazyBGW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles LazyBGW.DoWork
    'This code runs in the BGW-Thread
    'Perform the whole download task here or just call your 
    e.Result = DownloadPricat()
    'Work is done, put results in the eventargs-variable for further processing
End Sub

Private Sub LazyBGW_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles LazyBGW.RunWorkerCompleted
    'This code runs in the UI-Thread
    Dim results As Boolean = CBool(e.Result)
    MessageBox.Show("The worker is done and our result is: " & results.ToString)
End Sub
End Class

编辑: 在控制台应用程序中,您可以使用任务:

Module Module1
Private Function DownloadPricat() As Boolean
    Threading.Thread.Sleep(10000)
    Return True
End Function
Sub Main()
    Dim DLTask As New System.Threading.Tasks.Task(Of Boolean)(Function() DownloadPricat())
    DLTask.Start()
    Dim ThisTime As Date = Date.Now
    Console.Write("Downloading")
    While DLTask.IsCompleted = False AndAlso DLTask.IsCanceled = False AndAlso DLTask.IsFaulted = False
        If (Date.Now - ThisTime).TotalSeconds > 1 Then
            Console.Write(".")
            ThisTime = Date.Now
        End If
    End While
    Console.Write("Done.")
End Sub

结束模块