关闭DownloadFileAsync thread vb.net

时间:2016-09-26 13:28:14

标签: vb.net

我有一个ftp主机,我需要下载并解压缩一个zip文件。这是我当前代码的一个示例。当我尝试运行解压缩脚本时,我得到了该文件仍在另一个进程中使用的异常。如何正确关闭webclient downloadfileasync?

Imports System.ComponentModel
Imports System.Net

Public Class Form1
    Private wc As New WebClient()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        wc = New WebClient()

        AddHandler wc.DownloadProgressChanged, Sub(s As Object, ByVal e1 As DownloadProgressChangedEventArgs)
                                                Me.Invoke(New MethodInvoker(Sub() ProgressBar1.Value = e1.ProgressPercentage))
                                            End Sub

        AddHandler wc.DownloadFileCompleted, Sub(s As Object, e1 As ComponentModel.AsyncCompletedEventArgs)
        MsgBox("Complete")
        ZipFile.ExtractToDirectory(downloadPath & DagensXML, downloadPath)

                                            End Sub
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        wc.DownloadFileAsync(New Uri("ftp://user:pass@0.0.0.0/ESStatistikListeModtag/ESStatistikListeModtag-20160327-094743.zip"), "C:\XML\ESStatistikListeModtag-20160327-094743.zip")
    End Sub
End Class

0 个答案:

没有答案