Você está na página 1de 5

#Region "Imports" Imports System.Collections.Generic Imports System.

ComponentModel #End Region Public Class DownloadedFile #Region "Module level variables" Private Private Private Private Private #End Region #Region "Properties" Public Property PartName() As String Get Return m_sPartName End Get Set(ByVal value As String) m_sPartName = value End Set End Property Public Property PartNumber() As String Get Return m_sPartNumber End Get Set(ByVal value As String) m_sPartNumber = value End Set End Property Public Property FilePath() As String Get Return m_sFilePath End Get Set(ByVal value As String) m_sFilePath = value End Set End Property Public Property Placed() As Boolean Get Return m_bPlaced End Get Set(ByVal value As Boolean) m_bPlaced = value End Set End Property Public Property ErrorInfo() As String Get Return m_sErrorInfo m_sPartName As String = String.Empty m_sFilePath As String = String.Empty m_sErrorInfo As String = String.Empty m_sPartNumber As String m_bPlaced As Boolean

End Get Set(ByVal value As String) m_sErrorInfo = value End Set End Property #End Region End Class Public Class DownloadingFile #Region "Module level variables" Private m_sPartName As String = String.Empty Private m_sFileName As String = String.Empty Private m_sTargetLocation As String #End Region #Region "Properties" Public Property PartName() As String Get Return m_sPartName End Get Set(ByVal value As String) m_sPartName = value End Set End Property Public Property FileName() As String Get Return m_sFileName End Get Set(ByVal value As String) m_sFileName = value End Set End Property Public Property TargetLocation() As String Get Return m_sTargetLocation End Get Set(ByVal value As String) m_sTargetLocation = value End Set End Property #End Region End Class Public Class DownloadManager #Region "Class level variables" Private m_Vault As IVault Private m_DownloaderBackgroundWorker As New BackgroundWorker

Private m_Components As New Dictionary(Of String, DownloadedFile) Public Event DownloadCompleted(ByVal fileInfo As DownloadedFile) #End Region #Region "Constructor" Sub New(ByVal vault As IVault) m_Vault = vault InitializeBackGroundWorker() End Sub #End Region #Region "Methods" Private Sub InitializeBackGroundWorker() m_DownloaderBackgroundWorker.WorkerSupportsCancellation = True SubscribeEvents(True) End Sub Private Sub SubscribeEvents(ByVal bSubscribe As Boolean) If bSubscribe Then AddHandler m_DownloaderBackgroundWorker.DoWork, AddressOf DoWorkHand ler AddHandler m_DownloaderBackgroundWorker.RunWorkerCompleted, AddressO f DownloadCompletedHandler Else RemoveHandler m_DownloaderBackgroundWorker.DoWork, AddressOf DoWorkH andler RemoveHandler m_DownloaderBackgroundWorker.RunWorkerCompleted, Addre ssOf DownloadCompletedHandler End If End Sub Private Sub DoWorkHandler(ByVal sender As Object, ByVal e As DoWorkEventArgs ) System.Windows.Forms.Application.DoEvents() If m_DownloaderBackgroundWorker.WorkerReportsProgress Then e.Cancel = True Return End If Dim fileInfo As DownloadingFile = CType(e.Argument, DownloadingFile) e.Result = DownloadFile(fileInfo) End Sub Private Sub DownloadCompletedHandler(ByVal sender As Object, ByVal e As RunW orkerCompletedEventArgs) Dim downloadedFileInfo As DownloadedFile = CType(e.Result, DownloadedFil e) RaiseEvent DownloadCompleted(downloadedFileInfo) End Sub Private Function DownloadFile(ByVal fileInfo As DownloadingFile) As Download

edFile Dim downLoadedfile As New DownloadedFile Try downLoadedfile.PartName = fileInfo.PartName downLoadedfile.PartNumber = fileInfo.FileName Dim sFilePath As String = GetFullFilePath(fileInfo.TargetLocation, f ileInfo.FileName) If sFilePath = String.Empty Then If m_Vault IsNot Nothing Then Dim vault As New Vault(m_Vault.ConnectionSettings) downLoadedfile = vault.DownLoadFileFromVault(fileInfo) If downLoadedfile IsNot Nothing Then Return downLoadedfile Else downLoadedfile.FilePath = GetFullFilePath(fileInfo.Targe tLocation, fileInfo.FileName) End If End If Else downLoadedfile.FilePath = sFilePath End If Catch ex As Exception End Try Return downLoadedfile End Function Private Function GetFullFilePath(ByVal sVaultFolderPath As String, ByVal sfi lename As String) As String Dim sFiles() As String sFiles = System.IO.Directory.GetFiles(sVaultFolderPath, sfilename + ".*" , System.IO.SearchOption.TopDirectoryOnly) For Each file In sFiles Dim fi As New IO.FileInfo(file) If fi.Extension = ".ipt" Or fi.Extension = ".iam" Then Return file End If Next Return String.Empty End Function Public Sub StartAsync(ByVal fileInfo As DownloadingFile) m_DownloaderBackgroundWorker.RunWorkerAsync(fileInfo) End Sub Public Sub Start(ByVal fileInfo As DownloadingFile)

DownloadFile(fileInfo) End Sub Public Sub StopDownload() m_DownloaderBackgroundWorker.CancelAsync() End Sub #End Region End Class

Você também pode gostar